Search code examples
c#visual-studio-2013smovstest

VSTest.Console error: Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime


I get the following error when I manually run my unit test project from the command line using VSTest.Console, or when I run it as a build step from teamcity (also using vstest):

Mixed mode assembly is built against version ‘v2.0.50727′ of the runtime

Some googling shows that this is likely cause by the fact that the unit test project references another project that uses 'SMO' for dropping and creating a database.

The unit tests run fine when I run them from within visual studio. The error only occurs when I run them from the command line or from TeamCity. All related questions on stackoverflow suggest that the following should be added to a config file:

However, I have no idea which configuration file. I've tried adding it to the app.config file (which I manually copied to the bin directory), but that makes no difference. The config files that are used by visual studio have the correct startup attributes (which explains why it works when I run the tests from Visual Studio)

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.x86.exe.config (32-bit)

or

C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.executionengine.exe.config (64-bit)

Please help! How should I tell VSTest to "useLegacyV2RuntimeActivationPolicy="true""?


Solution

  • It turned out to be quite simple. The command I used to execute the tests from the commandline was:

    C:\...\TestProject> VSTest.Console \bin\Release\TestProject.dll
    

    The calling executable in this case is:

    C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe
    

    Which has a corresponding .config file vstest.console.exe.config in the same folder. I added:

    <startup useLegacyV2RuntimeActivationPolicy="true">
      <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
      <requiredRuntime version="v4.0.20506" />
    </startup>
    

    as suggested here and everything works.

    Update:

    This also works for me:

    <startup useLegacyV2RuntimeActivationPolicy="true" />