やる気駆動型エンジニアの備忘録

WPF(XAML+C#)の話題を中心に.NET/Android/CI やたまに趣味に関するブログです

JenkinsでxUnitを実行・レポート出力する方法

JenkinsでxUnitを実行したときに少しハマったのでメモメモ。
JenkinsでxUnitを使用している場合、MSTestと同じ方法では実行できません。
xUnitを使用するには、"xunit.runner.console"が必要です。
以下、実行環境です。

1.xUnitをNuGetパッケージから取得する。

NuGetで以下のパッケージを取得します。

  • xunit
  • xunit.runner.console

2.JenkinsでxUnit実行ジョブを設定する。

Jenkinsのジョブ設定で、ビルドから"Windowsバッチコマンドの実行"を追加して以下のコマンドを入力します。

::過去の結果があれば削除
rmdir /S /Q <レポート出力フォルダ>
mkdir <レポート出力フォルダ>
::xUnitは通常のMSTestではなく、以下の方法で実行する必要がある。
cd %WORKSPACE%\packages\xunit.runner.console.2.1.0\tools
xunit.console.exe "%WORKSPACE%<ユニットテストプロジェクトDLLパス>" -nunit "%WORKSPACE%<レポート出力フォルダ>\<レポートファイル名>.xml"

"xunit.console.exe"はxUnitをコンソールで実行するための実行ファイルです。
ちなみに、"-?"で以下のようなヘルプが表示されます。

Valid options:
  -nologo                : do not show the copyright message
  -nocolor               : do not output results with colors
  -noappdomain           : do not use app domains to run test code
  -failskips             : convert skipped tests into failures
  -parallel option       : set parallelization based on option
                         :   none        - turn off all parallelization
                         :   collections - only parallelize collections
                         :   assemblies  - only parallelize assemblies
                         :   all         - parallelize assemblies & collections
  -maxthreads count      : maximum thread count for collection parallelization
                         :   default   - run with default (1 thread per CPU thread)
                         :   unlimited - run with unbounded thread count
                         :   (number)  - limit task thread pool size to 'count'
  -noshadow              : do not shadow copy assemblies
  -wait                  : wait for input after completion
  -diagnostics           : enable diagnostics messages for all test assemblies
  -debug                 : launch the debugger to debug the tests
  -serialize             : serialize all test cases (for diagnostic purposes only)
  -trait "name=value"    : only run tests with matching name/value traits
                         : if specified more than once, acts as an OR operation
  -notrait "name=value"  : do not run tests with matching name/value traits
                         : if specified more than once, acts as an AND operation
  -method "name"         : run a given test method (should be fully specified;
                         : i.e., 'MyNamespace.MyClass.MyTestMethod')
                         : if specified more than once, acts as an OR operation
  -class "name"          : run all methods in a given test class (should be fully
                         : specified; i.e., 'MyNamespace.MyClass')
                         : if specified more than once, acts as an OR operation
  -namespace "name"      : run all methods in a given namespace (i.e.,
                         : 'MyNamespace.MySubNamespace')
                         : if specified more than once, acts as an OR operation

Reporters: (optional, choose only one)
  -appveyor              : forces AppVeyor CI mode (normally auto-detected)
  -quiet                 : do not show progress messages
  -teamcity              : forces TeamCity mode (normally auto-detected)
  -verbose               : show verbose progress messages

Result formats: (optional, choose one or more)
  -xml <filename>        : output results to xUnit.net v2 style XML file
  -xmlv1 <filename>      : output results to xUnit.net v1 style XML file
  -nunit <filename>      : output results to NUnit-style XML file
  -html <filename>       : output results to HTML file

Jenkinsには、xUnitでのレポート出力フォーマットに対応していないので、NUnitのフォーマットで出力します。
Publish HTML reportプラグインでレポートを確認したい場合は、HTML出力も追加してもいいかもしれません。

3.xUnitのレポート出力を設定する。

ここでは、予め"NUnit plugin"をインストールしておいてください。
ビルド後の処理から"Publish NUnit test result report"を選択し、以下の内容を設定します。

f:id:iyemon018:20170202170257j:plain

"Test report XMLs"には、出力したレポートファイルのパスを設定します。

あとはジョブを実行するとテスト結果が出力されるようになります。

f:id:iyemon018:20170202170720j:plain

カバレッジをレポートで出力する場合はこちらを参照してください。
iyemon018.hatenablog.com


めでたし、めでたし