C# 你如何从 Jenkins 运行 NUnit 测试?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9121312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 06:04:09  来源:igfitidea点击:

How do you run NUnit tests from Jenkins?

c#continuous-integrationhudsonjenkins

提问by blueberryfields

I'm looking to run automated NUnit tests for a C# application, nightly and on each commit to svn.

我希望在每晚和每次提交到 svn 时为 C# 应用程序运行自动 NUnit 测试。

Is this something that Jenkins-CI can do?
Is there an online tutorial or how-to document which documents a similar setup that I can look at?

这是 Jenkins-CI 可以做的事情吗?
是否有在线教程或操作说明文档,其中记录了我可以查看的类似设置?

采纳答案by Ralph Willgoss

I needed to do exactly what you do, here's how I setup Jenkins to do this:

我需要完全按照您的要求做,这是我设置 Jenkins 的方法:

  1. Add the NUnit Plugin to Jenkins
  2. In your project go to Configure-> Build-> Add a build step
  3. In the dropdown scroll down to -> Execute Windows Batch Command
  4. Ensure this step is placed after your MSBuild step
  5. Add the following, replacing the variables:
  1. 将 NUnit 插件添加到 Jenkins
  2. 在您的项目中,转到配置->构建->添加构建步骤
  3. 在下拉列表中向下滚动到 ->执行 Windows 批处理命令
  4. 确保此步骤位于 MSBuild 步骤之后
  5. 添加以下内容,替换变量:

Single dll test:

单个dll测试:

[PathToNUnit]\bin\nunit-console.exe [PathToTestDll]\Selenium.Tests.dll /xml=nunit-result.xml

[PathToNUnit]\bin\nunit-console.exe [PathToTestDll]\Selenium.Tests.dll /xml=nunit-result.xml

Multiple dll test using NUnit test projects:

使用NUnit 测试项目的多个 dll 测试:

[PathToNUnit]\bin\nunit-console.exe [PathToTests]\Selenium.Tests.nunit /xml=nunit-result.xml

[PathToNUnit]\bin\nunit-console.exe [PathToTests]\Selenium.Tests.nunit /xml=nunit-result.xml

  1. Under Post-build Actions, tick Publish NUnit test result report
  2. For the textbox Test report XMLs, enter nunit-result.xml
  1. Post-build Actions 下,勾选Publish NUnit test result report
  2. 对于文本框Test report XMLs,输入nunit-result.xml

Once you project has been built, NUNit will now run and the results will be viewable either on the Dashboard(if you hover over the Weather report icon) or on the project page under Last Test Result.

一旦您的项目被构建,NUIt 现在将运行并且结果将在仪表板(如果您将鼠标悬停在天气报告图标上)或项目页面上的 Last Test Result下可见。

You could also run the command from within Visual Studio or as part of you local build process.

您还可以从 Visual Studio 内或作为本地构建过程的一部分运行该命令。

Here's two blog posts I used for reference. I didn't find any that fitted my requirements exactly:
1-Hour Guide to Continuous Integration Setup: Jenkins meets .Net (2011)
Guide to building .NET projects using Hudson (2008)

这是我用来参考的两篇博客文章。我没有找到任何完全符合我的要求:
持续集成设置 1 小时指南:Jenkins 满足 .Net(2011)
使用 Hudson 构建 .NET 项目的指南(2008)

回答by Matt

Jenkins does have plugins that will support that. The exact configuration is going to depend quite a bit on your project setup. There are specific plugins for nUnit, MSBuild,nAnt etc. Start by looking at the plugins page, but it shouldn't be terribly difficult to figure out.

Jenkins 确实有插件支持。确切的配置将在很大程度上取决于您的项目设置。有适用于 nUnit、MSBuild、nAnt 等的特定插件。首先查看插件页面,但应该不难弄清楚。

回答by jglouie

This works nicely, I've set this up before.

这很好用,我以前设置过。

Configure NUnit to output the results to an XML file and configure the NUnit Jenkins Pluginto consume this XML file. The results will be available on the dashboard.

配置 NUnit 以将结果输出到 XML 文件,并配置NUnit Jenkins 插件以使用此 XML 文件。结果将显示在仪表板上。

Now, how you invoke NUnit is up to you. The way we did it was: Jenkins job executes NAnt target executes NUnit test suite.

现在,如何调用 NUnit 取决于您。我们这样做的方式是:Jenkins 作业执行 NAnt 目标执行 NUnit 测试套件。

You can configure Jenkins jobs to run on commit and/or scheduled at a certain time.

您可以将 Jenkins 作业配置为在提交和/或安排在特定时间运行。

回答by Akira Yamamoto

I think it's better to fail the build when it doesn't pass so you don't deploy it. Do something like this:

我认为最好在构建未通过时使构建失败,以便您不要部署它。做这样的事情:

C:\YourNUnitDir\nunit-console.exe C:\YourOutDir\YourLib.dll /noshadow
if defined ERRORLEVEL if %ERRORLEVEL% neq 0 goto fail_build

:: any other command

: fail_build
endlocal
exit %ERRORLEVEL%

Reference: http://www.greengingerwine.com/index.php/2013/01/tip-check-errorlevel-in-your-post-build-steps-when-using-nunit/

参考:http: //www.greengingerwine.com/index.php/2013/01/tip-check-errorlevel-in-your-post-build-steps-when-using-nunit/

回答by JCH2k

The solution from Ralph Willgoss is working good, but i changed 2 things to make it great:

来自 Ralph Willgoss 的解决方案运行良好,但我改变了 2 件事使其变得更好:

a) I used a NUnit project instead of the DLL file directly. This makes it more easy to add more assemblies or configure the test in the NUnit GUI.

a) 我直接使用了 NUnit 项目而不是 DLL 文件。这使得在 NUnit GUI 中添加更多程序集或配置测试变得更加容易。

b) I added one more line to the batch to prevent the build from failing when a test fails:

b) 我在批处理中又添加了一行,以防止在测试失败时构建失败:

[PathToNUnit]\bin\nunit-console.exe [PathToTestProject]\UnitTests.nunit /xml=nunit-result.xm
exit 0

The NUnit Plugin mentioned marks the build UNSTABLEautomatically, which is exactly what i want, whenever a test fails. It shows with a yellow dot.

提到的 NUnit 插件会自动标记构建不稳定,这正是我想要的,只要测试失败。它以黄点显示。

回答by Daniel McQuiston

If you don't want to hardcode your unit test projects, you are better off writing a script to grab all of your Unit Test project dll's. We do it with Powershell and follow a specific convention for naming our Unit Testing Projects. Here is the content of the powershell file that runs our unit tests:

如果您不想对单元测试项目进行硬编码,最好编写一个脚本来获取所有单元测试项目 dll。我们使用 Powershell 并遵循特定约定来命名我们的单元测试项目。以下是运行我们的单元测试的 powershell 文件的内容:

param(
[string] $sourceDirectory = $env:WORKSPACE
, $fileFilters = @("*.UnitTests.dll", "*_UnitTests.dll", "*UnitTests.dll")
, [string]$filterText = "*\bin\Debug*"
)

#script that executes all unit tests available.
$nUnitLog = Join-Path $sourceDirectory "UnitTestResults.txt"
$nUnitErrorLog = Join-Path $sourceDirectory "UnitTestErrors.txt"

Write-Host "Source: $sourceDirectory"
Write-Host "NUnit Results: $nUnitLog"
Write-Host "NUnit Error Log: $nUnitErrorLog"
Write-Host "File Filters: $fileFilters"
Write-Host "Filter Text: $filterText"

$cFiles = ""
$nUnitExecutable = "C:\Program Files (x86)\NUnit 2.6.3\bin\nunit-console-x86.exe"

# look through all subdirectories of the source folder and get any unit test assemblies. To avoid duplicates, only use the assemblies in the Debug folder
[array]$files = get-childitem $sourceDirectory -include $fileFilters -recurse | select -expand FullName | where {$_ -like $filterText}

foreach ($file in $files)
{
    $cFiles = $cFiles + $file + " "
}

# set all arguments and execute the unit console
$argumentList = @("$cFiles", "/framework:net-4.5", "/xml=UnitTestResults.xml")

$unitTestProcess = start-process -filepath $nUnitExecutable -argumentlist $argumentList -wait -nonewwindow -passthru -RedirectStandardOutput $nUnitLog -RedirectStandardError $nUnitErrorLog

if ($unitTestProcess.ExitCode -ne 0)
{
    "Unit Test Process Exit Code: " + $unitTestProcess.ExitCode
    "See $nUnitLog for more information or $nUnitErrorLog for any possible errors."
    "Errors from NUnit Log File ($nUnitLog):"
    Get-Content $nUnitLog | Write-Host
}

$exitCode = $unitTestProcess.ExitCode

exit $exitCode

The script is robust enough that we are reusing for all of our build jobs. If you don't like the full path to NUnit console, you could always put that location in your PATH environment variable.

该脚本足够强大,我们可以在所有构建作业中重复使用。如果您不喜欢 NUnit 控制台的完整路径,您始终可以将该位置放在您的 PATH 环境变量中。

Then we put the RunUnitTests.ps1 file on our build server and use this batch command:

然后我们将 RunUnitTests.ps1 文件放在我们的构建服务器上并使用这个批处理命令:

powershell.exe -file "{full-path-to-script-direcory}\RunUnitTests.ps1"

回答by MeJ

This is my solution for running OpenCoverwith vstestin Jenkins:

这是我在 Jenkins 中使用vstest运行OpenCover 的解决方案:

param(
[string] $sourceDirectory = $env:WORKSPACE
, $includedFiles = @("*Test.dll")
, $excludedFiles = @("*.IGNORE.dll")
, [string]$filterFolder = "*\bin\Debug*"
)

# Executables
$openCoverExecutable = "C:\Users\tfsbuild\AppData\Local\Apps\OpenCover\OpenCover.Console.exe"
$unitExecutable = "F:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow\vstest.console.exe"

# Logs
$openCoverReport = Join-Path $sourceDirectory "opencover.xml"
$openCoverFilter = "+[*]* -[*Test]*"

Write-Host "`r`n==== Configuration for executing tests ===="
Write-Host "Source: `"$sourceDirectory`""
Write-Host "Included files: `"$includedFiles`""
Write-Host "Excluded files: `"$excludedFiles`""
Write-Host "Folder filter: `"$filterFolder`""
Write-Host ""
Write-Host "OpenCover Report: `"$openCoverReport`""
Write-Host "OpenCover filter: `"$openCoverFilter`""

# look through all subdirectories of the source folder and get any unit test assemblies. To avoid duplicates, only use the assemblies in the Debug folder
[array]$files = get-childitem $sourceDirectory -include $includedFiles -exclude $excludedFiles -recurse | select -expand FullName | where {$_ -like $filterFolder} | Resolve-Path -Relative

$exitCode = 0
$failedTestDlls = ""

foreach ($file in $files)
{
    Write-Host "`r`nCurrent test dll: $file"

    # set all arguments and execute OpenCover
    $argumentList = @("-target:`"$unitExecutable`"", "-targetargs:`"$file /UseVsixExtensions:false /Logger:trx`"", "-register:user -filter:`"$openCoverFilter`" -mergeoutput -mergebyhash -skipautoprops -returntargetcode -output:`"$openCoverReport`"")

    $unitTestProcess = start-process -filepath $openCoverExecutable -argumentlist $argumentList -wait -nonewwindow -passthru -WorkingDirectory $sourceDirectory

    if ($unitTestProcess.ExitCode -ne 0)
    {
        $failedTestDlls = $failedTestDlls + $file + "`r`n"
        $exitCode = $unitTestProcess.ExitCode
    }
}

if ($exitCode -ne 0)
{
    Write-Host "`r`n==== Executing tests in following dlls failed ===="
    Write-Host "$failedTestDlls"
}

exit $exitCode

Each test dll is executed in an own process because we had troubles to execute all test dlls in a single procress (probmels with assembly loading).

每个测试 dll 都在自己的进程中执行,因为我们无法在单个进程中执行所有测试 dll(程序集加载的问题)。

回答by Winston33

For Nunit 3 or above farmework:

对于 Nunit 3 或以上的农活:

  1. Building Step (Windows command line) "c:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" c:\AutomationTraining\CSharpSelenium\bin\Debug\test.dll --result=TestR.xml;format=nunit2

  2. Post step for Nunit report publishing, it shows only test results file in Jenkins workspace directory, not in your project: TestR.xml

  1. 构建步骤(Windows 命令行) "c:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" c:\AutomationTraining\CSharpSelenium\bin\Debug\test.dll --result=TestR.xml;format=nunit2

  2. Nunit 报告发布的后期步骤,它仅显示 Jenkins 工作区目录中的测试结果文件,而不显示您的项目: TestR.xml

We need to make test results in nunit2 format because now Jenkins Nunit plugin doesn't recognize Nunit3 results format. Also options string format is different: --result=TestR.xml;format=nunit2NOT /xml=nunit-result.xml

我们需要以 nunit2 格式制作测试结果,因为现在 Jenkins Nunit 插件不识别 Nunit3 结果格式。选项字符串格式也不同: --result=TestR.xml;format=nunit2NOT /xml=nunit-result.xml

回答by stop-cran

For .Net Core it suffices to add "execute shell" build step with following script:

对于 .Net Core,只需使用以下脚本添加“执行 shell”构建步骤即可:

#!bash -x

cd $my_project_dir
rm -rf TestResults   # Remove old test results.
dotnet test -l trx

After that add "Publish MSTest test result report" post-build action to make test results visible.

之后添加“发布 MSTest 测试结果报告”构建后操作以使测试结果可见。

Default test reports path should be **/*.trxand will publish all produced .trxfiles.

默认测试报告路径应该是**/*.trx并将发布所有生成的.trx文件。