如何设置与opencover一起使用的MSTest的dll搜索路径?

How to set dll search path of MSTest used with opencover?

我的单元测试具有依赖项,这些依赖项来自项目外部目录中的 dll 池。我们的政策是通常将这些 dll(来自池)的链接放在项目目录中名为 "System" 的目录中,并将它们的 CopyToOutputDirectory 设置为 PreserveNewest。之后,在项目中我们添加了对池中 dll 的引用,但将其设置为 而不是 私有副本,这意味着它不会输出到构建目录。在 InitAssembly() 方法中,我们设置了 运行 时间通过 AppDomain.CurrentDomain.AssemblyResolve 也在 AppDomain.CurrentDomain.BaseDirectory, "System") 中搜索。

[TestClass]
public class SomeUnitTest : OurUnitTestBase
{
  [AssemblyInitialize]
  public static void AssemblyInit(TestContext context)
  {
    Debug.WriteLine("in AssemblyInit");
    base.InitAssembly();
  }
}

当从 VisualStudio 内部 运行 连接我们的代码以及从 VisualStudio 的 Test-Explorer 内部 运行 连接单元测试时(vstest.executionengine。x86.exe ).但是,当 运行 来自以下使用 opencover 执行覆盖率分析的批处理脚本时,MSTest 无法在系统子目录中找到 dll:

REM Bring dev tools into the PATH.
call "C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\Tools\VsDevCmd.bat"

mkdir .\CoverageAnalysisReport

REM Run unit tests through OpenCover
REM This allows OpenCover to gather code coverage results
.\..\..\Tools\CoverageAnalysis\opencover\OpenCover.Console.exe^
  -register:user^
  -target:MSTest.exe^  
  -targetargs:"/noresults /noisolation /testcontainer:..\bin\Debug\OurUnitTests.dll"^
  -filter:+[*]*^
  -output:.\CoverageAnalysisReport\output.xml

REM Generate the report
.\..\..\Tools\CoverageAnalysis\ReportGenerator\bin\ReportGenerator.exe^
  -reports:.\CoverageAnalysisReport\output.xml^
  -targetdir:.\CoverageAnalysisReport^
  -reporttypes:HTML^
  -filters:-OurUnitTests*

REM Open the report
start .\CoverageAnalysisReport\index.htm

生成的日志文件显示,none 单元测试可能是 运行,因此失败。此外它指出

Warning: The assembly "BaseLib" as a direct or indirect dependency of the test container "C:\MyProject\bin\debug\ourunittests.dll" was not found.

但我知道 [AssemblyInitialize] 被调用是因为我临时放在那里的 System.Windows.Form.MessageBox.Show(..) 实际上在执行 bat 文件期间显示了。

有没有办法让 MSTest 也搜索 System 子目录中的 dll?

我通过将以下内容作为名为 Local.testsettings 的文件的内容放在与批处理文件相同的目录中(我在其中修改了 -targetargs:"/testcontainer:\"..\bin\x86\Debug\OurUnitTests.dll\" /testSettings:\".\Local.testsettings\" 行)解决了这个问题:

<?xml version="1.0" encoding="UTF-8"?>
<TestSettings name="Lokal" id="2fa4344c-1f2f-4a04-86f3-41d223b10333" xmlns="http://microsoft.com/schemas/VisualStudio/TeamTest/2010">
  <Description>Dies sind die standardmäßigen Testeinstellungen für einen lokalen Testlauf.</Description>
  <Deployment>
    <DeploymentItem filename="..\bin\Debug\System\" />
  </Deployment>
  <Execution>
    <TestTypeSpecific>
      <UnitTestRunConfig testTypeId="13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b">
        <AssemblyResolution>
          <TestDirectory useLoadContext="true" />
        </AssemblyResolution>
      </UnitTestRunConfig>
    </TestTypeSpecific>
    <AgentRule name="Execution Agents">
    </AgentRule>
  </Execution>
</TestSettings>