从命令行运行mstest时TypeInitializationException

时间:2010-07-28 05:47:38

标签: .net unit-testing automated-tests mstest

我们有一个单元测试的子集,直到最近才一直通过Visual Studio运行。我现在正致力于持续集成设置,它将运行测试。当通过Visual Studio运行时,所有测试都会通过,但是当我尝试使用mstest从命令行运行它们时,它们会失败并显示“TypeInitializationException”,表示找不到该类型的dll文件。

System.TypeInitializationException: The type initializer for foo.bar_Accessor' threw an exception. 
--->  System.IO.FileNotFoundException: Could not load file or assembly 'foo, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
The system cannot find the file specified.Assembly manager loaded from: 
c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll

有问题的dll是单元测试项目中的项目参考。还有一个测试参考,用于创建类的foo.bar_Accesor版本。

我注意到当通过visual studio运行测试时,会创建一个包含IN和OUT文件夹的“Coverage _timestamp”文件夹。 OUT文件夹包含foo.dll和foo_accesor.dll等。

从命令行运行测试时,会创建一个“username _timestamp”文件夹,其中只包含一个OUT文件夹。 OUT文件夹包含foo_accesor.dll,但不包含错误消息中提到的foo.dll。

1 个答案:

答案 0 :(得分:0)

我设法通过/noisolation开关为我们解决了这个问题。此开关描述为:

  

在MSTest.exe进程中运行测试。此选择可提高测试运行速度,但会增加MSTest.exe进程的风险。

我的MSBuild脚本现在看起来像:

<Target Name="Test" DependsOnTargets="Compile">
  <PropertyGroup>
    <TestSuccessOrNot>1</TestSuccessOrNot>
  </PropertyGroup>
      <Exec Command='"$(VS100COMNTOOLS)..\IDE\mstest.exe" /noisolation /testcontainer:"C:\path\to\test.dll"' >
    <Output TaskParameter="ExitCode" PropertyName="TestSuccessOrNot"/>
  </Exec>
  <Error Condition="$(TestSuccessOrNot) == 1" Text="Unit tests fail!"/>
</Target>