跳至主要內容

Junit 報告器

一個 WebdriverIO 報告器,可建立與 Jenkins 相容的基於 XML 的 JUnit 報告

安裝

最簡單的方法是透過以下方式,將 @wdio/junit-reporter 保留在您的 package.json 中作為開發依賴項

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 點狀報告器和 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} 將傳回 [物件物件]。您必須指定要在檔案名稱中使用的功能屬性。

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 內設定各種錯誤通知組合。
如果 Jasmine 測試類似於 expect(true).toBe(false, 'my custom message'),您將會收到此測試錯誤

{
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