1) Change the output path for Test projects
The first thing to do is to change the output path for the test project (or projects). We do this so that the testing binaries to are separated from the release binaries. Below is a screen shot of the directory being changed inside visual studio.Things to note:
- We selected the “Automoated_Build” configuration that was created in the first blog post “Create a Build File for a Visual Studio Solution - MsBuild Series”
- Used a relative path the the build directory
2) NUnit Target
For running our tests I am going to use the “MsBuld.Community.Tasks” NUnit Task. We have been using other Tasks from the “MsBuld.Community.Tasks” so we already have the targets and dll that are needed for these tasks (see this post for details on getting the Community Tasks setup).So, now we just define our NUnit target as follows:
<Target Name="NUnit"> <ItemGroup> <TestAssembly Include="$(BuildOutputDir)\Test\*.test.dll" /> </ItemGroup> <Message Text="NUnit is running on: @(TestAssembly)" /> <Nunit ToolPath=".\Tools\NUnit" Assemblies="@(TestAssembly)" /> </Target> <Target Name="Test" DependsOnTargets="NUnit"> <Message Text="Testing code" /> </Target>
Things to note:
- I have created two targets “Test” & “NUnit” which is not necessary if your only using NUnit. However, I did this so that it's easy to add other targets for different testing frameworks such as MbUnit, xUnit, Mspec etc. (if you using multiple test frameworks make sure you checkout gallio)
- The TestAssembly ItemGroup will grab any assembly that ends with Test.
- The nunit console runner and lib directory have been copied to "[Project Dir]\Tools\NUnit"
The necessary nunit binaries have been copied into “[Project Root]\Tools\Nunit”. Normally I just copy “nunit-console.exe” , “nunit-console.exe.config” and the entire Lib directory.
3) Incorporate the Test target into the main build file
We now have the Test target so we all we need to do is have our default build target depend on our Test target, as follows:<Target Name="Build" DependsOnTargets="Clean; VersionSolutionInfo; Compile; Test; Zip-Source; Zip-Binaries; NuPack">Now when we run our build the code is tested before creating our zips or NuPack (or should I say Nuget).
<Message Text="Clean, VersionSolutionInfo, Compile, Test, Zip-Source, Zip-Binaries, NuPack"/> </Target>
NB: Please check the reference files for full copies of the build targets used in this series so far
Resources:
- Main MsBulid File - Main build with targets : clean, compile & build. Imports all other build files.
- Version MsBuild File - Contains targets from a previous post (to auto version AssemblyInfo from mercurial)
- Zip MsBuild File - Contains targets from a previous post (to create zips)
- NuPack MsBuild File - Contains targets from a previous post (to create a nupack package).
- Test MsBuild File - Contains the targets from this post (nunit testing)
- MSBuild Project File Schema Reference - Microsoft documents on MsBuild
This is the fifth post in the MsBuild series.
- Create a Build File for a Visual Studio Solution - MsBuild Series
- Mercurial Revision No to Version your AssemblyInfo - MsBuild Series
- Zipping Build Outputs - MsBuild Series
- NuPack packaging - MsBuild Series
- NUnit Testing - MsBuild Series
- Web Projects and AspNetComplier - MsBuild Series (coming later)
- Cruise Control - MsBuild Series (coming later)