When I build a Visual Studio 2010 project, I want to run unit tests with NUnit and display test results only when some tests have failed.
I have setup a post-build event in Visual Studio to call a batch file like below:
$(ProjectDir)RunUnitTest.bat "$(SolutionDir)packages\NUnit.Runners.2.6.0.12051\tools\nunit-console.exe" "$(TargetPath)"
Then in RunUnitTest.bat, I call nunit-console.exe and pass in the test project dll.
@echo off
REM runner is the full path to nunit-console.exe
set runner=%1
REM target is the full path to the dll containing unit tests
set target=%2
"%runner%" "%target%"
if errorlevel 1 goto failed
if errorlevel 0 goto passed
:failed
echo some tests failed
goto end
:passed
echo all tests passed
goto end
:end
echo on
After that, NUnit generates TestResult.xml containing test results, so how do I display it in user friendly way? It’ll be the best if it displays inside Visual Studio, but other options are open too.
I ended up using nunit-summary to generate all pass summary html reports and nunit-results to create failed test reports in html.
This approach is quiet easy to setup.
First, download nunit-summary and nunit-results from launchpad and put them in TestRunner folder under the test project.
Then, add a post-build event to call a batch file.
Lastly, add the batch file to TestRunner folder under test project. It should contain the following files at the least:
nunit-results.exenunit-results.tests.dllnunit-results.tests.pdbnunit-summary.exenunit.util.dllRunUnitTests.batPost-build event for the project containing unit tests:
Scripts in
RunUnitTest.bat