如何运行Jenkins的NUnit测试

本文关键字:Jenkins NUnit 测试 运行 何运行 | 更新日期: 2023-09-27 18:22:13

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

这是詹金斯CI可以做的事情吗
有没有一个在线教程或操作文档,其中记录了我可以查看的类似设置?

如何运行Jenkins的NUnit测试

我需要做你所做的事情,下面是我如何设置Jenkins来做到这一点:

  1. 将NUnit插件添加到Jenkins
  2. 在您的项目中,转到配置->构建>添加构建步骤
  3. 在下拉菜单中向下滚动至->执行Windows批处理命令
  4. 确保此步骤位于MSBuild步骤之后
  5. 添加以下内容,替换变量:

单个dll测试:

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

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

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

  1. 生成后操作下,勾选发布NUnit测试结果报告
  2. 对于测试报告XMLs文本框,输入nunit-result.xml

一旦您的项目构建完成,NUNit现在将运行,结果将在仪表板上(如果您将鼠标悬停在天气报告图标上)或上次测试结果下的项目页面上查看。

您也可以在VisualStudio中运行该命令,或者作为本地生成过程的一部分运行该命令。

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

如果你不想硬编码你的单元测试项目,你最好写一个脚本来获取你的所有单元测试项目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

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

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

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

对于Nunit 3或以上农场作业:

  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

我们需要以nunit2格式生成测试结果,因为现在Jenkins-Nunit插件无法识别Nunit3结果格式。此外,选项字符串格式不同:--result=TestR.xml;format=nunit2不是/xml=nunit-result.xml

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

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

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

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

Ralph Willgoss的解决方案运行良好,但我改变了两件事使其变得很棒:

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

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

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

上面提到的NUnit插件在测试失败时自动标记构建UNSTABLE,这正是我想要的。它显示为一个黄点。

我认为最好在构建未通过时失败,这样你就不会部署它

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%

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

Jenkins确实有支持这一点的插件。确切的配置将在很大程度上取决于您的项目设置。nUnit、MSBuild、nAnt等都有特定的插件。从插件页面开始,但应该不太难弄清楚。

这是我在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

每个测试dll都在自己的进程中执行,因为我们在一个进程中执行所有测试dll时遇到了问题(加载程序集时出现问题)。

对于.Net Core,添加以下脚本的"执行shell"构建步骤就足够了:

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

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

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