簡體   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