Cutter+Jenkinsを使ったC++のテスト(2)
前回の続きで、cutterとJenkinsを連携させる方法についてです。
cutterは--xml-report=出力先ファイル名
とオプション指定することでテスト結果をXMLに出力できます。ですがこの内容は独自のルールで、JenkinsのxUnit Pluginで管理できるフォーマットとは異なります。Jenkins上でテスト結果の傾向も管理したいので、どうにかしてこのデータをxUnitの出力形式に変換しなければいけません。
とはいえ面倒なので、誰か変換する仕組みをすでに作ってくれていないかな…と思ったら、下のサイトでxUnit系に変換できるxslを公開してくれていました。ありがたい話です。
上から入手できるcutter-to-junit.xslファイルは、cutterの出力XMLと一緒にxslproc
コマンドに渡すことで、目的のフォーマットに整形されたファイルを作成できます。
# リダイレクトでファイルに出力 xsltproc cutter-to-junit.xsl cutter-result.xml > xunit-result.xml
xsltproc
コマンドは、OS Xだとはじめから/usr/bin
にインストールされているので、特に追加インストールは必要ありませんでした。というわけで、「Cutter+Jenkinsを使ったC++のテスト(1) - swift life」で使ったプログラムとテストで試してみました。すると、元のデータ:
<report> <result> <test-case> <name>test</name> <start-time>2015-12-21T15:24:29.830856Z</start-time> <elapsed>0.000065</elapsed> </test-case> <test> <name>test::test_add</name> <start-time>2015-12-21T15:24:29.830939Z</start-time> <elapsed>0.000065</elapsed> </test> <status>success</status> <start-time>2015-12-21T15:24:29.830939Z</start-time> <elapsed>0.000065</elapsed> </result> <result> <test-case> <name>test</name> <start-time>2015-12-21T15:24:29.830856Z</start-time> <elapsed>0.000411</elapsed> </test-case> <test> <name>test::test_sub</name> <start-time>2015-12-21T15:24:29.831512Z</start-time> <elapsed>0.000361</elapsed> </test> <status>failure</status> <detail><3 == klass.sub(5,2)> expected: <3> actual: <10></detail> <backtrace> <entry> <file>test_MyClass.cpp</file> <line>12</line> <info>void test::test_sub(): cppcut_assert_equal(3, klass.sub(5,2), )</info> </entry> </backtrace> <start-time>2015-12-21T15:24:29.831512Z</start-time> <elapsed>0.000068</elapsed> <expected>3</expected> <actual>10</actual> </result> </report>
が次のように変換されました。
<?xml version="1.0"?> <testsuites errors="0" failures="1" tests="2" name=""> <testcase classname="test" name="test::test_add" time="0.000065" timestamp="2015-12-21T15:24:29.830939Z"/> <testcase classname="test" name="test::test_sub" time="0.000068" timestamp="2015-12-21T15:24:29.831512Z"> <failure message="void test::test_sub(): cppcut_assert_equal(3, klass.sub(5,2), )" type="Failure"> File: test_MyClass.cpp Line: 12</failure> </testcase> </testsuites>
いい感じです。これをJenkinsに組み込めばうまくいけそうです。