> For the complete documentation index, see [llms.txt](https://tdd.shujuwajue.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tdd.shujuwajue.com/ju-ti-shi-jian/javascript-ce-shi-kuang-jia/mocha-junit-reporter.md).

# mocha-junit-reporter

[mocha-junit-reporter npm地址](https://www.npmjs.com/package/mocha-junit-reporter)

[mocha-junit-reporter github地址](https://github.com/michaelleeallen/mocha-junit-reporter#readme)

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

## 安装

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

或者作为一个全局模块

```javascript
$ npm install -g mocha-junit-reporter
```

运行mocha与 mocha-junit-reporter：

```javascript
$ mocha test --reporter mocha-junit-reporter
```

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

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

或

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

或

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

## 向testsuite追加属性

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

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

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

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

或

```javascript
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`选项.

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

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

```javascript
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 the`testsuites`tag (defaults to 'Mocha Tests')                |
