@128technology/mocha-junit-reporter 中文文档教程

发布于 4年前 浏览 19 项目主页 更新于 3年前

JUnit Reporter for Mocha

构建状态npm

生成 JUnit 样式的 XML 测试结果。

Installation

$ npm install mocha-junit-reporter --save-dev

或者作为全局模块

$ npm install -g mocha-junit-reporter

Usage

使用 mocha-junit-reporter 运行 mocha:

$ mocha test --reporter mocha-junit-reporter

这将在 ./test-results.xml 输出结果文件。 您可以选择通过设置声明结果 XML 文件的备用位置 环境变量 MOCHA_FILE 或在 reporterOptions 中指定 mochaFile

$ MOCHA_FILE=./path_to_your/file.xml mocha test --reporter mocha-junit-reporter

或者

$ mocha test --reporter mocha-junit-reporter --reporter-options mochaFile=./path_to_your/file.xml

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        mochaFile: './path_to_your/file.xml'
    }
});

Append properties to testsuite

可以在 testsuite 下向报告添加属性. 如果您希望您的 CI 环境向报告添加额外的构建道具以用于分析目的,这将很有用

<testsuites>
  <testsuite>
    <properties>
      <property name="BUILD_ID" value="4291"/>
    </properties>
    <testcase/>
    <testcase/>
    <testcase/>
  </testsuite>
</testsuites>

。这样做可以通过环境变量传递它们:

PROPERTIES=BUILD_ID:4291 mocha test --reporter mocha-junit-reporter

或者

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        properties: {
            BUILD_ID: 4291
        }
    }
})

Results Report

结果 XML 文件名可以包含 [hash],例如 ./path_to_your/test-results.[hash].xml. [hash] 替换为测试结果 XML 的 MD5 哈希。 这样就可以支持并行执行多个mocha-junit-reporter的写入测试结果在单独的文件中。

为了显示完整的套件标题(包括父级),只需指定 testsuitesTitle 选项

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        testsuitesTitle: true,
        suiteTitleSeparatedBy: '.' // suites separator, default is space (' '), or period ('.') in jenkins mode
    }
});

。 >testCaseSwitchClassnameAndName 记者选项。

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        testCaseSwitchClassnameAndName: true
    }
});

以下是使用 testCaseSwitchClassnameAndName 选项时的 XML 输出示例:

valueXML output
true<testcase name="should behave like so" classname="Super Suite should behave like so">
false (default)<testcase name="Super Suite should behave like so" classname="should behave like so">

您还可以通过设置 reporterOptions.testsuitesTitle 和通过设置 reporterOptions.rootSuiteTitle 获取根套件的 name 属性。

System out and system err

JUnit 格式定义了一对标签 - - 用于描述测试生成的输出 和错误流,分别。 可以将测试输出/错误作为文本行数组传递:

it ('should report output', function () {
  this.test.consoleOutputs = [ 'line 1 of output', 'line 2 of output' ];
});
it ('should report error', function () {
  this.test.consoleErrors = [ 'line 1 of errors', 'line 2 of errors' ];
});

因为这个模块只是一个报告器而不是一个独立的测试运行器,它不执行 输出捕获本身。 因此,测试的作者有责任提供一种机制 通过它填充输出/错误数组。

如果只捕获 console.log/console.error 是一个选项,一个简单的(如果有点 hack-ish)解决方案是替换 这些函数的全局实现,如下所示:

var util = require('util');

describe('my console tests', function () {
  var originalLogFunction = console.log;
  var originalErrorFunction = console.error;
  beforeEach(function _mockConsoleFunctions() {
    var currentTest = this.currentTest;
    console.log = function captureLog() {
      var formattedMessage = util.format.apply(util, arguments);
      currentTest.consoleOutputs = (currentTest.consoleOutputs || []).concat(formattedMessage);
    };
    console.error = function captureError() {
      var formattedMessage = util.format.apply(util, arguments);
      currentTest.consoleErrors = (currentTest.consoleErrors || []).concat(formattedMessage);
    };
  });
  afterEach(function _restoreConsoleFunctions() {
    console.log = originalLogFunction;
    console.error = originalErrorFunction;
  });
  it('should output something to the console', function() {
    // This should end up in <system-out>:
    console.log('hello, %s', 'world');
  });
});

如果您想要 XML 格式的测试输出,请记住使用 --reporter-options outputs=true 运行。

Attachments

启用 attachments 配置选项将允许在 JUnit Attachments Plugin< 中附加文件和屏幕截图/a> 格式。

附件路径可以注入到测试对象

it ('should include attachment', function () {
  this.test.attachments = ['/absolut/path/to/file.png'];
});

中 XML 输出如下所示:

<system-out>output line 1
output line 2
[[ATTACHMENT|path/to/file]]</system-out>

Full configuration options

ParameterDefaultEffect
mochaFiletest-results.xmlconfigures the file to write reports to
includePendingfalseif set to a truthy value pending tests will be included in the report
propertiesnulla hash of additional properties to add to each test suite
toConsolefalseif set to a truthy value the produced XML will be logged to the console
useFullSuiteTitlefalseif set to a truthy value nested suites' titles will show the suite lineage
suiteTitleSeparatedBy (space)the character to use to separate nested suite titles. (defaults to ' ', '.' if in jenkins mode)
testCaseSwitchClassnameAndNamefalseset to a truthy value to switch name and classname values
rootSuiteTitleRoot Suitethe name for the root suite. (defaults to 'Root Suite')
testsuitesTitleMocha Teststhe name for the testsuites tag (defaults to 'Mocha Tests')
outputsfalseif set to truthy value will include console output and console error output
attachmentsfalseif set to truthy value will attach files to report in JUnit Attachments Plugin format (after console outputs, if any)
antModefalseset to truthy value to return xml compatible with Ant JUnit schema
antHostnameprocess.env.HOSTNAMEhostname to use when running in antMode will default to environment HOSTNAME
jenkinsModefalseif set to truthy value will return xml that will display nice results in Jenkins

JUnit Reporter for Mocha

Build Statusnpm

Produces JUnit-style XML test results.

Installation

$ npm install mocha-junit-reporter --save-dev

or as a global module

$ npm install -g mocha-junit-reporter

Usage

Run mocha with mocha-junit-reporter:

$ mocha test --reporter mocha-junit-reporter

This will output a results file at ./test-results.xml. You may optionally declare an alternate location for results XML file by setting the environment variable MOCHA_FILE or specifying mochaFile in reporterOptions:

$ MOCHA_FILE=./path_to_your/file.xml mocha test --reporter mocha-junit-reporter

or

$ mocha test --reporter mocha-junit-reporter --reporter-options mochaFile=./path_to_your/file.xml

or

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        mochaFile: './path_to_your/file.xml'
    }
});

Append properties to testsuite

You can also add properties to the report under testsuite. This is useful if you want your CI environment to add extra build props to the report for analytics purposes

<testsuites>
  <testsuite>
    <properties>
      <property name="BUILD_ID" value="4291"/>
    </properties>
    <testcase/>
    <testcase/>
    <testcase/>
  </testsuite>
</testsuites>

To do so pass them in via env variable:

PROPERTIES=BUILD_ID:4291 mocha test --reporter mocha-junit-reporter

or

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        properties: {
            BUILD_ID: 4291
        }
    }
})

Results Report

Results XML filename can contain [hash], e.g. ./path_to_your/test-results.[hash].xml. [hash] is replaced by MD5 hash of test results XML. This enables support of parallel execution of multiple mocha-junit-reporter's writing test results in separate files.

In order to display full suite title (including parents) just specify testsuitesTitle option

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        testsuitesTitle: true,
        suiteTitleSeparatedBy: '.' // suites separator, default is space (' '), or period ('.') in jenkins mode
    }
});

If you want to switch classname and name of the generated testCase XML entries, you can use the testCaseSwitchClassnameAndName reporter option.

var mocha = new Mocha({
    reporter: 'mocha-junit-reporter',
    reporterOptions: {
        testCaseSwitchClassnameAndName: true
    }
});

Here is an example of the XML output when using the testCaseSwitchClassnameAndName option:

valueXML output
true<testcase name="should behave like so" classname="Super Suite should behave like so">
false (default)<testcase name="Super Suite should behave like so" classname="should behave like so">

You can also configure the testsuites.name attribute by setting reporterOptions.testsuitesTitle and the root suite's name attribute by setting reporterOptions.rootSuiteTitle.

System out and system err

The JUnit format defines a pair of tags - <system-out/> and <system-err/> - for describing a test's generated output and error streams, respectively. It is possible to pass the test outputs/errors as an array of text lines:

it ('should report output', function () {
  this.test.consoleOutputs = [ 'line 1 of output', 'line 2 of output' ];
});
it ('should report error', function () {
  this.test.consoleErrors = [ 'line 1 of errors', 'line 2 of errors' ];
});

Since this module is only a reporter and not a self-contained test runner, it does not perform output capture itself. Thus, the author of the tests is responsible for providing a mechanism via which the outputs/errors array will be populated.

If capturing only console.log/console.error is an option, a simple (if a bit hack-ish) solution is to replace the implementations of these functions globally, like so:

var util = require('util');

describe('my console tests', function () {
  var originalLogFunction = console.log;
  var originalErrorFunction = console.error;
  beforeEach(function _mockConsoleFunctions() {
    var currentTest = this.currentTest;
    console.log = function captureLog() {
      var formattedMessage = util.format.apply(util, arguments);
      currentTest.consoleOutputs = (currentTest.consoleOutputs || []).concat(formattedMessage);
    };
    console.error = function captureError() {
      var formattedMessage = util.format.apply(util, arguments);
      currentTest.consoleErrors = (currentTest.consoleErrors || []).concat(formattedMessage);
    };
  });
  afterEach(function _restoreConsoleFunctions() {
    console.log = originalLogFunction;
    console.error = originalErrorFunction;
  });
  it('should output something to the console', function() {
    // This should end up in <system-out>:
    console.log('hello, %s', 'world');
  });
});

Remember to run with --reporter-options outputs=true if you want test outputs in XML.

Attachments

enabling the attachments configuration option will allow for attaching files and screenshots in JUnit Attachments Plugin format.

Attachment path can be injected into the test object

it ('should include attachment', function () {
  this.test.attachments = ['/absolut/path/to/file.png'];
});

If both attachments and outputs are enabled, and a test injects both consoleOutputs and attachments, then the XML output will look like the following:

<system-out>output line 1
output line 2
[[ATTACHMENT|path/to/file]]</system-out>

Full configuration options

ParameterDefaultEffect
mochaFiletest-results.xmlconfigures the file to write reports to
includePendingfalseif set to a truthy value pending tests will be included in the report
propertiesnulla hash of additional properties to add to each test suite
toConsolefalseif set to a truthy value the produced XML will be logged to the console
useFullSuiteTitlefalseif set to a truthy value nested suites' titles will show the suite lineage
suiteTitleSeparatedBy (space)the character to use to separate nested suite titles. (defaults to ' ', '.' if in jenkins mode)
testCaseSwitchClassnameAndNamefalseset to a truthy value to switch name and classname values
rootSuiteTitleRoot Suitethe name for the root suite. (defaults to 'Root Suite')
testsuitesTitleMocha Teststhe name for the testsuites tag (defaults to 'Mocha Tests')
outputsfalseif set to truthy value will include console output and console error output
attachmentsfalseif set to truthy value will attach files to report in JUnit Attachments Plugin format (after console outputs, if any)
antModefalseset to truthy value to return xml compatible with Ant JUnit schema
antHostnameprocess.env.HOSTNAMEhostname to use when running in antMode will default to environment HOSTNAME
jenkinsModefalseif set to truthy value will return xml that will display nice results in Jenkins
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文