繁体   English   中英

你如何从Jenkins运行NUnit测试?

[英]How do you run NUnit tests from Jenkins?

我正在寻找为C#应用程序运行自动NUnit测试,每晚和每次提交到svn。

这是Jenkins-CI能做的吗?
是否有在线教程或如何记录哪些文档类似我可以查看的设置?

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

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

单个dll测试:

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

使用NUnit测试项目进行多个dll测试:

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

  1. Post-build Actions下 ,勾选Publish NUnit测试结果报告
  2. 对于文本框测试报告XML ,输入nunit-result.xml

构建完项目后,NUNit现在将运行,结果将在仪表板上显示(如果将鼠标悬停在“天气”报告图标上)或“ 上次测试结果”下的项目页面

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

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

如果您不想对单元测试项目进行硬编码,那么最好编写一个脚本来获取所有单元测试项目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或以上的farmework:

  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 NOT /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中使用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都在一个自己的进程中执行,因为我们在单个procress中执行所有测试dll时遇到麻烦(带有程序集加载的probmels)。

詹金斯确实有支持它的插件。 确切的配置将取决于您的项目设置。 有针对nUnit,MSBuild,nAnt等的特定插件。首先查看插件页面,但要弄清楚它应该非常困难。

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

#!bash -x

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM