TypeInitializationException when running mstest from command line
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.
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>
精彩评论