Setting up NUnit for TFSBuild
Read this if you are using an existing build controller:
Previous Test Adapters needed to be installed as custom dll's (Version control custom assemblies on your build controller). This is not needed anymore, you can just add the test adapter as a nuget package to your test project. However, if you have custom assemblies, they take precedence over the nuget package dll's, so it's recommended that you check this folder for older versions.
Single test project
Install the following NuGet package: NUnit TestAdapter including NUnit 2.6.3 framework 1.2.0.
Multiple test projects
We're using Nunit because it has "better" support than other frameworks like XUnit on TFSBuild.
Running tests for a specific category
Each unit test framework has a different way of grouping Unit tests in categories (MSTest uses "TestCategory", Nunit uses "Category", XUnit uses "Traits", Chutzpah uses "Module", ...). Some test frameworks have adapters available that map the TFS Build properties to the properties used by your framework. However, In my experience, they only work for NUnit (and of course MSTest).
On TFS Build when you create a new build definition you can specify what test category to use by filling the "Test case filter". This filter uses the following syntax: "TestCategory=CategoryName". There are 4 filter expression keys that you can use: "FullyQualifiedName, Name, Priority, TestCategory" and you can specify multiple by using the pipe character (ex: "TestCategory=Unit|TestCategory!=Acceptance").
Running tests with code coverage
Change your the build definition to have CodeCoverageEnabled enabled
When we run our build, we get the following result:
When you click on "Coverage Result" on the successful build, you can download the Code Coverage file. This file needs to be opened in Visual Studio. You can then have a detailed view of the coverage. If you double click a tree node, you see the actual code fragment that was covered (it's highlighted in blue).
In the last part of this blogseries we have discussed how to use another framework than MSTest to run our tests on TFSBuild. We see that it's not only MSTest that delivers support for TestCategories and Code Coverage but you could use another framework like NUnit too.
Sample Solution Files:
Download [22.5 MB]