【问题标题】:How to display NUnit test results only when some tests failed?如何仅在某些测试失败时显示 NUnit 测试结果?
【发布时间】:2012-07-03 09:34:05
【问题描述】:

当我构建一个 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 中显示,但也可以打开其他选项。

【问题讨论】:

  • 我强烈建议您简单地使用 VS 集成测试运行器:TestDriven.Net 在 VS 输出窗格中显示测试结果,而NCrunch 在您编写代码时自动(并且有选择地)运行所有测试。

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    这种方法很容易设置。

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

    然后,添加构建后事件以调用批处理文件。

    最后,将批处理文件添加到测试项目下的 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
    

    【讨论】:

      猜你喜欢
      • 2017-12-08
      • 1970-01-01
      • 1970-01-01
      • 2018-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      相关资源
      最近更新 更多