If you want to run NUnit tests with MSBuild, then you need to download and install the MSBuild Community tasks. This allows you to create a unit test target like this:
<ItemGroup> <TestAssembly Include="ClientBin\*Tests.dll" /> </ItemGroup> <Target Name="Test" DependsOnTargets="Build"> <NUnit Assemblies="@(TestAssembly)" WorkingDirectory="." ToolPath="C:\Program Files\NUnit 2.5.5\bin\net-2.0" /> </Target>
(Most examples don’t show the need to specify a ToolPath, but I have found I need it, perhaps because it isn’t in my Path?)
The trouble is, if the assemblies containing the unit tests have been build with the .NET 4 framework, you will get the following error:
C:\Program Files\NUnit 2.5.5\bin\net-2.0\nunit-console.exe /nologo ClientBin\ Nice.Inform.Client.Tests.dll ProcessModel: Default DomainUsage: Single Execution Runtime: net-2.0 Unhandled Exception: System.BadImageFormatException: Could not load file or assembly 'D:\TFS\Trial \Inform5\ClientBin\Client.Tests.dll' or one of its dependencies. This assembly is built by a runtime newer than the currently loaded runtime a nd cannot be loaded. File name: 'D:\TFS\Trial\Inform5\ClientBin\Client.Tests.dll'
What is needed is to ensure that nunit-console.exe runs against the .NET 4 framework. The way I achieved this was to make a copy of the net-2.0 folder that comes with NUnit 2.5.5 and rename it to net-4.0 (n.b. make sure the lib folder comes along too as nunit-console.exe depends on its contents). Then, I edited the nunit-console.exe.config file to have the following contents (the key bit is to add the supportedRuntime setting):
<?xml version="1.0" encoding="utf-8"?> <configuration> <!-- Set the level for tracing NUnit itself --> <!-- 0=Off 1=Error 2=Warning 3=Info 4=Debug --> <system.diagnostics> <switches> <add name="NTrace" value="0" /> </switches> </system.diagnostics> <startup> <supportedRuntime version="v4.0"/> </startup> <runtime> <!-- We need this so test exceptions don't crash NUnit --> <legacyUnhandledExceptionPolicy enabled="1" /> <!-- Look for addins in the addins directory for now --> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <probing privatePath="lib;addins"/> </assemblyBinding> </runtime> </configuration>
Now all that is needed is to update the ToolPath in the MSBuild script to point to the new location
<Target Name="Test" DependsOnTargets="Build"> <NUnit Assemblies="@(TestAssembly)" WorkingDirectory="." ToolPath="C:\Program Files\NUnit 2.5.5\bin\net-4.0" /> </Target>
I’d be interested in hearing if there is an easier way of solving this problem though, as I don’t really want to have to require all developers to manually perform these steps on their machine.
5 comments:
When I faced that, I just decided to switch to the "exec" task.
This allowed me to add the /framework argument easily, like so:
/framework=4.0.30319
thanks for this suggestion Travis, will give this a try
Hi Travis - thanks for the post. Saved me a couple of hours.
Thanks It worked
I'm using the MSBuild sprinter to run my assessments, but with the deficiency of xunit.net assistance have no way to show the outcomes.
Post a Comment