メインコンテンツにスキップ

Junit レポーター

Jenkins 互換の XML ベースの JUnit レポートを作成する WebdriverIO レポーター

インストール

最も簡単な方法は、`@wdio/junit-reporter` を `package.json` の devDependency として保持することです。

npm install @wdio/junit-reporter --save-dev

`WebdriverIO` のインストール方法については、こちらをご覧ください。

出力

このレポーターは、ランナーごとにレポートを出力します。そのため、各スペックファイルごとに XML レポートを受け取ります。以下は、スペックファイルのさまざまなシナリオにおける XML 出力の例です。

単一の describe ブロック

describe('a test suite', () => {
it('a test case', function () {
// do something
// assert something
});
});

は以下のように変換されます

<testsuites>
<testsuite name="a test suite" timestamp="2019-04-18T13:45:21" time="11.735" tests="0" failures="0" errors="0" skipped="0">
<properties>
<property name="specId" value="0"/>
<property name="suiteName" value="a test suite"/>
<property name="capabilities" value="chrome"/>
<property name="file" value=".\test\specs\asuite.spec.js"/>
</properties>
<testcase classname="chrome.a_test_case" name="a_test_suite_a_test_case" time="11.706"/>
</testsuite>
</testsuites>

ネストされた describe ブロック

describe('a test suite', () => {
describe('a nested test suite', function() {
it('a test case', function () {
// do something
// assert something
});
});
});

は以下のように変換されます

<testsuites>
<testsuite name="a test suite" timestamp="2019-04-18T13:45:21" time="11.735" tests="0" failures="0" errors="0" skipped="0">
<properties>
<property name="specId" value="0"/>
<property name="suiteName" value="a test suite"/>
<property name="capabilities" value="chrome"/>
<property name="file" value=".\test\specs\asuite.spec.js"/>
</properties>
</testsuite>
<testsuite name="a nested test suite" timestamp="2019-04-18T13:45:21" time="11.735" tests="0" failures="0" errors="0" skipped="0">
<properties>
<property name="specId" value="0"/>
<property name="suiteName" value="a nested test suite"/>
<property name="capabilities" value="chrome"/>
<property name="file" value=".\test\specs\asuite.spec.js"/>
</properties>
<testcase classname="chrome.a_test_case" name="a nested test suite a test case" time="11.706"/>
</testsuite>
</testsuites>

複数の describe ブロック

describe('a test suite', () => {
it('a test case', function () {
// do something
// assert something
});
});
describe('a second test suite', () => {
it('a second test case', function () {
// do something
// assert something
});
});

は以下のように変換されます

<testsuites>
<testsuite name="a test suite" timestamp="2019-04-18T13:45:21" time="11.735" tests="0" failures="0" errors="0" skipped="0">
<properties>
<property name="specId" value="0"/>
<property name="suiteName" value="a test suite"/>
<property name="capabilities" value="chrome"/>
<property name="file" value=".\test\specs\asuite.spec.js"/>
<testcase classname="chrome.a_test_case" name="a nested test suite a test case" time="11.706"/>
</properties>
</testsuite>
<testsuite name="a second test suite" timestamp="2019-04-18T13:45:21" time="11.735" tests="0" failures="0" errors="0" skipped="0">
<properties>
<property name="specId" value="0"/>
<property name="suiteName" value="a second test suite"/>
<property name="capabilities" value="chrome"/>
<property name="file" value=".\test\specs\asuite.spec.js"/>
</properties>
<testcase classname="chrome.a_second_test_case" name="a_second_test_suite_a_second_test_case" time="11.706"/>
</testsuite>
</testsuites>

失敗とエラー

すべてのテストケースの失敗は、JUnit テストケースのエラーとしてマップされます。アサーションの失敗またはエラーによるテストケースの失敗は、次のようになります。

<testcase classname="chrome.a_test_case" name="a_test_suite_a_test_case" time="0.372">
<failure message="Error: some error"/>
<system-err>
<![CDATA[
Error: some assertion failure
at UserContext.<anonymous> (C:\repo\webdriver-example\test\specs/a_test_suite.spec.js:22:17)
]]>
</system-err>
</testcase>

設定

次のコードは、デフォルトの wdio テストランナー設定を示しています。配列にレポーターとして `'junit'` を追加するだけです。テスト中に何らかの出力を受け取るには、WDIO Dot レポーター と WDIO JUnit レポーターを同時に実行できます。

// wdio.conf.js
module.exports = {
// ...
reporters: [
'dot',
['junit', {
outputDir: './',
outputFileFormat: function(options) { // optional
return `results-${options.cid}.${options.capabilities}.xml`
}
}]
],
// ...
};

以下のオプションがサポートされています

outputDir

xml ファイルを保存するディレクトリを定義します。

タイプ: `String`
必須

outputFileFormat

テスト実行後に作成される xml ファイルを定義します。

タイプ: `Object`
デフォルト: `function (opts) { return `wdio-${this.cid}-${name}-reporter.log` }`

outputFileFormat: function (options) {
return 'mycustomfilename.xml';
}

注: `options.capabilities` は、そのランナーの機能オブジェクトであるため、文字列で `${options.capabilities}` を指定すると [Object object] が返されます。ファイル名に含める機能のプロパティを指定する必要があります。

suiteNameFormat

テストスイート名(出力 xml など)のフォーマットにカスタム正規表現を提供する機能を提供します。

タイプ: `Regex`
デフォルト: `/[^a-zA-Z0-9@]+/`

// wdio.conf.js
module.exports = {
// ...
reporters: [
'dot',
['junit', {
outputDir: './',
suiteNameFormat: /[^a-zA-Z0-9@]+/
outputFileFormat: function(options) { // optional
return `results-${options.cid}.${options.capabilities}.xml`
}
}]
],
// ...
};

addFileAttribute

各テストケースにファイル属性を追加します。この設定は、主に CircleCI 用です。この設定は、より詳細な情報を提供しますが、他の CI プラットフォームでは機能しない場合があります。

タイプ: `Boolean`
デフォルト: `false`

packageName

`'packageName'` を設定することにより、パッケージを別のレベルで分割できます。たとえば、異なる環境変数を設定したテストスイートを反復処理する場合

タイプ: `String`

// wdio.conf.js
module.exports = {
// ...
reporters: [
'dot',
['junit', {
outputDir: './',
packageName: process.env.USER_ROLE // chrome.41 - administrator
}]
]
// ...
};

errorOptions

xml 内にさまざまな組み合わせのエラー通知を設定できます。
`expect(true).toBe(false, 'my custom message')` のような Jasmine テストの場合、このテストエラーが発生します

{
matcherName: 'toBe',
message: 'Expected true to be false, \'my custom message\'.',
stack: 'Error: Expected true to be false, \'my custom message\'.\n at UserContext.it (/home/mcelotti/Workspace/WebstormProjects/forcebeatwio/test/marco/prova1.spec.js:3:22)',
passed: false,
expected: [ false, 'my custom message' ],
actual: true
}

したがって、*どの*キーを*どこで*使用するかを選択できます。以下の例を参照してください。

タイプ: `Object`
デフォルト: `errorOptions: { error: "message" }`

// wdio.conf.js
module.exports = {
// ...
reporters: [
'dot',
['junit', {
outputDir: './',
errorOptions: {
error: 'message',
failure: 'message',
stacktrace: 'stack'
}
}]
],
// ...
};

addWorkerLogs

オプションのパラメーター。レポーターのテストからコンソールログを添付するには、このパラメーターを true に設定します。

タイプ: `Boolean`
デフォルト: `false`

// wdio.conf.js
module.exports = {
// ...
reporters: [
'dot',
['junit', {
outputDir: './',
addWorkerLogs: true
}]
],
// ...
};

テストケースにカスタムプロパティを追加する

このプラグインは、関数 `addProperty(name, value)` を提供します。この関数は、現在実行中のテストステップに追加の junit テストケースプロパティを追加するために使用できます。これらのプロパティは、結果の xml で `<property name="${name}" value="${value}" />` として報告されます。

これの典型的なユースケースは、問題またはテストケースへのリンクを追加することです。

使用例

mocha の例

import { addProperty } from '@wdio/junit-reporter'

describe('Suite', () => {
it('Case', () => {
addProperty('test_case', 'TC-1234')
})
})

Jenkins のセットアップ

最後に、CI ジョブ(Jenkins など)に xml ファイルの場所を指示する必要があります。そのためには、テストの実行後に実行されるポストビルドアクションをジョブに追加し、Jenkins(または必要な CI システム)を XML テスト結果にポイントします。

Point Jenkins to XML files

CI システムにそのようなポストビルドステップがない場合、インターネットのどこかにプラグインがある可能性があります。


WebdriverIO の詳細については、ホームページをご覧ください。

ようこそ!何かお困りですか?

WebdriverIO AI Copilot