【问题标题】:TypeInitializationException when running mstest from command line从命令行运行 mstest 时出现 TypeInitializationException
【发布时间】:2010-07-28 05:47:37
【问题描述】:

我们有一个单元测试的子集,直到最近一直通过 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 运行时,会创建一个“Coverage _timestamp”文件夹,其中包含一个 IN 和一个 OUT 文件夹。 OUT 文件夹包含 foo.dll 和 foo_accesor.dll 等内容。

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

【问题讨论】:

  • 遇到同样的问题。找到解决方案了吗?

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


【解决方案1】:

我设法用/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>

【讨论】:

    猜你喜欢
    • 2011-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-10
    • 2012-10-02
    • 2012-08-14
    相关资源
    最近更新 更多