program tip

Jenkins에서 NUnit 테스트를 어떻게 실행합니까?

radiobox 2020. 8. 12. 08:07
반응형

Jenkins에서 NUnit 테스트를 어떻게 실행합니까?


C # 응용 프로그램에 대해 밤마다 그리고 svn에 대한 각 커밋에 대해 자동화 된 NUnit 테스트를 실행하려고합니다.

Jenkins-CI가 할 수있는 일입니까?
내가 볼 수있는 유사한 설정을 문서화하는 온라인 자습서 또는 방법 문서가 있습니까?


나는 당신이하는 일을 정확히해야했습니다.이 작업을 수행하도록 Jenkins를 설정하는 방법은 다음과 같습니다.

  1. Jenkins에 NUnit 플러그인 추가
  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. 아래 빌드 후 작업 , 틱 NUnit과 테스트 결과 보고서를 게시
  2. Test report XMLs 텍스트 상자에 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 환경 변수에 넣을 수 있습니다.

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

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

For Nunit 3 or above farmework:

  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

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=nunit2 NOT /xml=nunit-result.xml


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

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

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

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


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

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.

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

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

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


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/


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.


This is my solution for running OpenCover with vstest in Jenkins:

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).


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

#!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.

Default test reports path should be **/*.trx and will publish all produced .trx files.

참고URL : https://stackoverflow.com/questions/9121312/how-do-you-run-nunit-tests-from-jenkins

반응형