测试驱动开发(TDD)
  • 介绍
  • 思想实践
    • 开发现状
    • 单元测试与功能测试的区别
    • 7个测试驱动开发的最佳实践
    • 掌握测试驱动开发的技术
    • 白话结语
  • 初步实践
    • 什么时候写测试?
    • Javascript 测试框架
      • Chai
      • Mocha
      • mocha-junit-reporter
      • SuperTest
      • Sinon
      • mockserver
      • Jasmine
      • Qunit
  • 具体实践
    • 收集目前测试问题整理
    • 改变项目开发方式
    • 对微服务Api的实践
Powered by GitBook
On this page
  • 安装
  • 向testsuite追加属性
  • 结果报告
  • 全配置选项

Was this helpful?

  1. 初步实践
  2. Javascript 测试框架

mocha-junit-reporter

PreviousMochaNextSuperTest

Last updated 6 years ago

Was this helpful?

生成 JUnit-style 的XML测试结果。

安装

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

或者作为一个全局模块

$ npm install -g mocha-junit-reporter

运行mocha与 mocha-junit-reporter:

$ mocha test --reporter mocha-junit-reporter

这将在./test-results.xml输出结果文件。可以通过设置环境变量``MOCHA_FILE```或在mochaFile`中指定reporterOptions,可选地设置XML文件结果的替代位置:

$ 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'
    }
});

向testsuite追加属性

还可以在testsuite下向报表添加属性。如果您希望您的CI环境为分析目的添加报表的额外构建道具,这将非常有用。

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

这样做通过env变量传递它们:

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

或

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

结果报告

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

为了显示完整的组别标题(包括parents),只需指定testsuitesTitle选项.

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

如果希望切换生成的testCase XML条目的classname和name,可以使用testCaseSwitchClassnameAndName选项。

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

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

value

XML 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来配置testsuites.name属性,并通过设置reporterOptions.testsuitesTitle来配置根suite的name属性。

全配置选项

Parameter

Effect

mochaFile

configures the file to write reports to

includePending

if set to a truthy value pending tests will be included in the report

properties

a hash of additional properties to add to each test suite

toConsole

if set to a truthy value the produced XML will be logged to the console

useFullSuiteTitle

if set to a truthy value nested suites' titles will show the suite lineage

suiteTitleSeparedBy

the character to use to separate nested suite titles. (defaults to ' ')

testCaseSwitchClassnameAndName

set to a truthy value to switch name and classname values

rootSuiteTitle

the name for the root suite. (defaults to 'Root Suite')

testsuitesTitle

the name for thetestsuitestag (defaults to 'Mocha Tests')

mocha-junit-reporter npm地址
mocha-junit-reporter github地址