我希望每晚和每次提交svn时都为C#应用程序运行自动化的NUnit测试。
Jenkins-CI可以做到这一点吗?
是否有在线教程或操作指南文档,其中记录了我可以查看的类似设置?
我希望每晚和每次提交svn时都为C#应用程序运行自动化的NUnit测试。
Jenkins-CI可以做到这一点吗?
是否有在线教程或操作指南文档,其中记录了我可以查看的类似设置?
Answers:
我需要做的正是您要做的,这是我设置Jenkins的方法:
单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
建立项目后,NUNit现在将运行,并且结果将显示在仪表板上(如果将鼠标悬停在“天气”报告图标上)或“ 最后测试结果”下的项目页面上。
您也可以在Visual Studio中运行该命令,也可以在本地构建过程中运行该命令。
这是我参考的两个博客文章。我没有找到完全符合我要求的内容:
1小时持续集成设置指南:Jenkins符合.Net(2011)
使用Hudson构建.NET项目的指南(2008)
"C:\Program Files (x86)\NUnit 2.6.3\bin\nunit-console-x86.exe" UnitTests/UnitTests.nunit
。为我完美地工作。
如果您不想对单元测试项目进行硬编码,最好编写一个脚本来捕获所有单元测试项目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"
[string] $sourceDirectory = $(get-location)
为空格,并且必须将路径更改为nUnit,以将路径更改为$cFiles = $cFiles + '"' + $file + '"' + " "
对于Nunit 3或更高版本的农作:
构建步骤(Windows命令行)
"c:\Program Files (x86)\NUnit.org\nunit-console\nunit3-console.exe" c:\AutomationTraining\CSharpSelenium\bin\Debug\test.dll --result=TestR.xml;format=nunit2
发布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)的解决方案效果很好,但我更改了2件事以使其出色:
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是在自己的进程中执行的,因为我们很难在单个proc中执行所有测试dll(带有程序集加载的问题)。