从命令行运行 mstest 时出现 TypeInitializationException

发布于 2024-09-12 02:42:01 字数 900 浏览 0 评论 0原文

我们有一个单元测试子集,直到最近它们一直通过 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 等。

当从命令行运行测试时,会创建一个“用户名_时间戳”文件夹,其中仅包含一个 OUT 文件夹。 OUT 文件夹包含 foo_accesor.dll 等内容,但不包含错误消息中提到的 foo.dll。

We have a subset of unit tests which until recently have always been run through Visual Studio. I am now working on a continuous integration setup which will run the tests. All of the tests pass when run through Visual Studio but when I attempt to run them from the command line using mstest they fail with a "TypeInitializationException" which says that it cannot find the dll file for the type.

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

The dll in question is a project reference in the unit test project. There is also a test reference which creates foo.bar_Accesor version of the class.

I've noticed that when the tests are run through visual studio a "Coverage _timestamp" folder is created which contains an IN and an OUT folder. The OUT folder contains, among other things, foo.dll and foo_accesor.dll.

When the tests are run from the command line a "username _timestamp" folder is created which contains only an OUT folder. The OUT folder contains, among other things, foo_accesor.dll but not foo.dll which is the one mentioned in the error message.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

简单爱 2024-09-19 02:42:01

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

I managed to solve it for us with the /noisolation switch. This switch is described as:

Run tests within the MSTest.exe process. This choice improves test run speed but increases risk to the MSTest.exe process.

My MSBuild script now looks something like:

<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文