如何在某些测试失败时显示NUnit测试结果?

时间:2012-07-02 23:42:52

标签: visual-studio-2010 unit-testing automation nunit

当我构建Visual Studio 2010项目时,我希望使用NUnit运行单元测试,并仅在某些测试失败时显示测试结果。

我在Visual Studio中设置了一个后期构建事件来调用如下的批处理文件:

$(ProjectDir)RunUnitTest.bat "$(SolutionDir)packages\NUnit.Runners.2.6.0.12051\tools\nunit-console.exe" "$(TargetPath)"

然后在RunUnitTest.bat中,我调用nunit-console.exe并传入测试项目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

之后,NUnit生成包含测试结果的TestResult.xml,那么如何以用户友好的方式显示它?如果它在Visual Studio中显示,它将是最好的,但其他选项也是打开的。

2 个答案:

答案 0 :(得分:0)

您可能需要考虑使用XSLT执行转换并显示TestResult.xml的结果。

答案 1 :(得分:0)

我最终使用nunit-summary生成所有通过摘要html报告,并使用nunit-results在html中创建失败的测试报告。

这种方法很容易安装。

首先,从启动板下载nunit-summary和nunit-results,并将它们放在测试项目下的TestRunner文件夹中。

然后,添加一个post-build事件来调用批处理文件。

最后,将批处理文件添加到测试项目下的TestRunner文件夹中。它至少应包含以下文件:

  • nunit-results.exe
  • nunit-results.tests.dll
  • nunit-results.tests.pdb
  • nunit-summary.exe
  • `NUnit的-core.dll
  • nunit.util.dll
  • RunUnitTests.bat

包含单元测试的项目的构建后事件:

"$(ProjectDir)TestRunner\RunUnitTests.bat" "$(SolutionDir)packages\NUnit.Runners.2.6.0.12051\tools\nunit-console.exe" "$(TargetPath)" "$(TargetDir)"

RunUnitTest.bat

中的脚本
REM     This batch file does the followings:
REM     1. runs unit test with nunit-console.exe and produces a TestResult.xml
REM     2. if one or more tests failes, it calls unit-results.exe to convert TestResult.xml to 
REM     Usage: RunUnitTests.bat "path-to-nunit.exe" "path-to-test.dll" "path-to-output-folder"

@echo off

REM get input arguments
set runner=%1
set target=%2
set output=%3

REM remove double quotes
set runner=%runner:"=%
set target=%target:"=%
set output=%output:"=%

REM prepare and clean up TestResult folder
if not exist "%output%TestResults\nul" md "%output%TestResults"
del "%output%\TestResults\*.*" /q

"%runner%" "%target%"

if errorlevel 1 goto failed
if errorlevel 0 goto passed

:failed
echo some tests failed
"%~dp0nunit-results.exe" "%output%TestResult.xml"
"%output%TestResults\index.html"
exit 1

:passed
echo all tests passed
"%~dp0nunit-summary.exe" "%output%TestResult.xml" -out=TestResults\TestSummary.html
"%output%TestResults\TestSummary.html"
exit 0
相关问题