开发者

Automated Build - Stop Building if NUnit Tests Fail

I have a simple batch file that calls MSBuild to build my Visual Studio solution, then runs the solution's NUnit tests, and then uses Wix to create an MSI. However I can't figure out how to stop the build process if any of the unit tests fail.

I tried the following:

nunit-console ../开发者_Python百科test.nunit
echo %ERRORLEVEL%

But regardless of whether unit tests passed or failed, %ERRORLEVEL% is always 0. Is there another way to easily determine whether any of the tests failed? I am using NUnit 2.5.9.


Call me lazy, but I just use MSBuildTasks and their NUnit Task. Then it's just a matter of setting the ContinueOnError property how you want it.

<NUnit ContinueOnError="true" Assemblies="@(TestAssemblies)" OutputXmlFile="$(TestResultsRoot)\nunit_results.xml" ToolPath="$(TeamBuildDependenciesPath)\NUnit\">
      <Output TaskParameter="ExitCode" PropertyName="NUnitResult" />
</NUnit>


After a while of searching it turns out the problem was that somewhere in my development process, the ERRORLEVEL variable was assigned a value. Thus when I wrote echo %ERRORLEVEL% it was reporting the ERRORLEVEL variable, not the actual ERRORLEVEL exit code.

I fixed this simply by closing the console and reopening it. Now when unit tests fail, the exit code is greater than 0. If all tests pass, the exit code is equal to 0.

EDIT: I completely removed this from my script. Now instead I put nunit-console $(TargetPath) in my test project's post-build events. That way, when I build the solution with MSBuild, it will automatically run unit tests and report an error when tests fail. When building in Visual Studio, this approach also causes an error to be added to the error list when unit tests fail, which is nice for identifying new bugs even more quickly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜