Friday, September 14, 2012

Running .Net 3.5 MSTest unit tests with Gallio.

Like all projects of a certain size there comes a time when there is a desire to have a dash board to quickly see the current state of affairs. 

For the dash board there is a growing open source solution called Sonar (http://www.sonarsource.org/) that is able to run all manner of plugins.  Perfect!

One of the supported plugins for .Net projects is another open source project called Gallio (http://www.gallio.org/).  Gallio has the ability to run all manner of unit test frameworks (MSTest, NUnit ,etc.) under a common API.  Nice!

One small gotcha:  Out of the box Gallio won't run .Net 3.5 unit test projects :(

The errors that were encountered were:

  • Mixed mode assembly is built against version 'v2.0.50727'
  • This method explicitly uses CAS policy, which has been obsoleted by ... please use the NetFx40_LegacySecurityPolicy configuration switch.
  • The plugin enable condition was not satisfied. Please note that this is the intended behavior for plugins that must be hosted inside third party applications in order to work. Enable condition: '${process:DEVENV.EXE} or ${process:VSTESTHOST.EXE} or ${process:QTAGENT.EXE} or ${process:QTAGENT32.EXE} or ${process:QTDCAGENT.EXE} or ${process:QTDCAGENT32.EXE} or {process:MSTEST.EXE}'
After much researching I found the solution.

Enable the 2008 Test Runner Plugin

By default the “Visual Studio 2008 Integration Shell” and “Visual Studio 2008 Test Runner plugin” are disable due to a failing prerequisite check.
To enable the plugins remove the “enableCondition” attribute and its value from the following files:

C:\Program Files\Gallio\bin\VisualStudio\v9.0\
  • Gallio.VisualStudio.Tip90.plugin
  • Gallio.VisualStudio.Shell90.plugin 

Enable Testing of unit tests authored in .Net 3.5

The command line utility “Gallio.Echo.exe” is a .Net 4.0 application that Sonar uses to run the unit tests.  This application needs to be configured to allow .Net 3.5 policies.

Update Gallio.Echo.exe.config to enable the testing of .Net 3.5 unit tests.

1).  Update the "runtime" configuration section to include the element: <NetFx40_LegacySecurityPolicy enabled="true"/>.
Example:
<runtime>
    <!-- Don't kill application on first uncaught exception.
         We don't want the test runner to terminate itself unexpectedly
         without reporting the test failure associated with that exception. -->
    <legacyUnhandledExceptionPolicy enabled="1" />

    <!-- Enable loading assemblies over the network in .Net 4.0 -->
    <loadFromRemoteSources enabled="true" />

    <NetFx40_LegacySecurityPolicy enabled="true"/>
  </runtime>

2).  Update the "startup" configuration section to include the attribute useLegacyV2RuntimeActivationPolicy="true"
Example:
<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0.30319" />
    <supportedRuntime version="v2.0.50727" />
  </startup>

   
Now Sonar is running all of the unit test projects, providing test and code coverage reports.  Life is good!