unit_test_reports.md 14,0 КБ
Newer Older
GitLab Bot's avatar
GitLab Bot включено в состав коммита
1
2
---
stage: Verify
GitLab Bot's avatar
GitLab Bot включено в состав коммита
3
group: Pipeline Insights
GitLab Bot's avatar
GitLab Bot включено в состав коммита
4
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
GitLab Bot's avatar
GitLab Bot включено в состав коммита
5
6
---

GitLab Bot's avatar
GitLab Bot включено в состав коммита
7
# Unit test reports **(FREE)**
GitLab Bot's avatar
GitLab Bot включено в состав коммита
8
9
10
11
12

> - [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/issues/45318) in GitLab 11.2. Requires GitLab Runner 11.2 and above.
> - [Renamed](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/39737) from JUnit test reports to Unit test reports in GitLab 13.4.

It is very common that a [CI/CD pipeline](pipelines/index.md) contains a
GitLab Bot's avatar
GitLab Bot включено в состав коммита
13
test job that verifies your code.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
14
If the tests fail, the pipeline fails and users get notified. The person that
GitLab Bot's avatar
GitLab Bot включено в состав коммита
15
works on the merge request has to check the job logs and see where the
GitLab Bot's avatar
GitLab Bot включено в состав коммита
16
17
tests failed so that they can fix them.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
18
You can configure your job to use Unit test reports, and GitLab displays a
GitLab Bot's avatar
GitLab Bot включено в состав коммита
19
report on the merge request so that it's easier and faster to identify the
GitLab Bot's avatar
GitLab Bot включено в состав коммита
20
failure without having to check the entire log. Unit test reports currently
GitLab Bot's avatar
GitLab Bot включено в состав коммита
21
22
only support test reports in the JUnit report format.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
23
If you don't use merge requests but still want to see the unit test report
GitLab Bot's avatar
GitLab Bot включено в состав коммита
24
25
output without searching through job logs, the full
[Unit test reports](#viewing-unit-test-reports-on-gitlab) are available
GitLab Bot's avatar
GitLab Bot включено в состав коммита
26
27
28
29
in the pipeline detail view.

Consider the following workflow:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
30
1. Your default branch is rock solid, your project is using GitLab CI/CD and
GitLab Bot's avatar
GitLab Bot включено в состав коммита
31
32
33
34
35
36
37
38
39
40
41
   your pipelines indicate that there isn't anything broken.
1. Someone from your team submits a merge request, a test fails and the pipeline
   gets the known red icon. To investigate more, you have to go through the job
   logs to figure out the cause of the failed test, which usually contain
   thousands of lines.
1. You configure the Unit test reports and immediately GitLab collects and
   exposes them in the merge request. No more searching in the job logs.
1. Your development and debugging workflow becomes easier, faster and efficient.

## How it works

GitLab Bot's avatar
GitLab Bot включено в состав коммита
42
First, GitLab Runner uploads all [JUnit report format XML files](https://www.ibm.com/docs/en/adfz/developer-for-zos/14.1.0?topic=formats-junit-xml-format)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
43
as [artifacts](yaml/artifacts_reports.md#artifactsreportsjunit) to GitLab. Then, when you visit a merge request, GitLab starts
GitLab Bot's avatar
GitLab Bot включено в состав коммита
44
45
comparing the head and base branch's JUnit report format XML files, where:

GitLab Bot's avatar
GitLab Bot включено в состав коммита
46
- The base branch is the target branch (usually the default branch).
GitLab Bot's avatar
GitLab Bot включено в состав коммита
47
48
49
50
- The head branch is the source branch (the latest pipeline in each merge request).

The reports panel has a summary showing how many tests failed, how many had errors
and how many were fixed. If no comparison can be done because data for the base branch
GitLab Bot's avatar
GitLab Bot включено в состав коммита
51
is not available, the panel just shows the list of failed tests for head.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
52
53
54
55
56
57
58
59
60

There are four types of results:

1. **Newly failed tests:** Test cases which passed on base branch and failed on head branch
1. **Newly encountered errors:** Test cases which passed on base branch and failed due to a
   test error on head branch
1. **Existing failures:**  Test cases which failed on base branch and failed on head branch
1. **Resolved failures:**  Test cases which failed on base branch and passed on head branch

GitLab Bot's avatar
GitLab Bot включено в состав коммита
61
62
Each entry in the panel shows the test name and its type from the list
above. Clicking on the test name opens a modal window with details of its
GitLab Bot's avatar
GitLab Bot включено в состав коммита
63
64
65
66
execution time and the error output.

![Test Reports Widget](img/junit_test_report.png)

GitLab Bot's avatar
GitLab Bot включено в состав коммита
67
68
### Number of recent failures

GitLab Bot's avatar
GitLab Bot включено в состав коммита
69
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/241759) in merge requests in GitLab 13.7.
Robert Speicher's avatar
Robert Speicher включено в состав коммита
70
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/268249) in GitLab 13.8.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
71
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/235525) in Test Reports in GitLab 13.9.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
72
73
74
75

If a test failed in the project's default branch in the last 14 days, a message like
`Failed {n} time(s) in {default_branch} in the last 14 days` is displayed for that test.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
76
77
## How to set it up

GitLab Bot's avatar
GitLab Bot включено в состав коммита
78
To enable the Unit test reports in merge requests, you must add
GitLab Bot's avatar
GitLab Bot включено в состав коммита
79
[`artifacts:reports:junit`](yaml/artifacts_reports.md#artifactsreportsjunit)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
80
81
82
83
84
85
86
87
88
in `.gitlab-ci.yml`, and specify the path(s) of the generated test reports.
The reports must be `.xml` files, otherwise [GitLab returns an Error 500](https://gitlab.com/gitlab-org/gitlab/-/issues/216575).

In the following examples, the job in the `test` stage runs and GitLab
collects the Unit test report from each job. After each job is executed, the
XML reports are stored in GitLab as artifacts and their results are shown in the
merge request widget.

To make the Unit test report output files browsable, include them with the
GitLab Bot's avatar
GitLab Bot включено в состав коммита
89
90
[`artifacts:paths`](yaml/index.md#artifactspaths) keyword as well, as shown in the [Ruby example](#ruby-example).
To upload the report even if the job fails (for example if the tests do not pass), use the [`artifacts:when:always`](yaml/index.md#artifactswhen)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
keyword.

You cannot have multiple tests with the same name and class in your JUnit report format XML file.

### Ruby example

Use the following job in `.gitlab-ci.yml`. This includes the `artifacts:paths` keyword to provide a link to the Unit test report output file.

```yaml
## Use https://github.com/sj26/rspec_junit_formatter to generate a JUnit report format XML file with rspec
ruby:
  stage: test
  script:
    - bundle install
    - bundle exec rspec --format progress --format RspecJunitFormatter --out rspec.xml
  artifacts:
    when: always
    paths:
      - rspec.xml
    reports:
      junit: rspec.xml
```

### Go example

GitLab Bot's avatar
GitLab Bot включено в состав коммита
116
Use the following job in `.gitlab-ci.yml`:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
117
118

```yaml
GitLab Bot's avatar
GitLab Bot включено в состав коммита
119
## Use https://github.com/gotestyourself/gotestsum to generate a JUnit report format XML file with go
GitLab Bot's avatar
GitLab Bot включено в состав коммита
120
121
122
golang:
  stage: test
  script:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
123
124
    - go get gotest.tools/gotestsum
    - gotestsum --junitfile report.xml --format testname
GitLab Bot's avatar
GitLab Bot включено в состав коммита
125
126
127
128
129
130
131
132
133
134
135
136
137
  artifacts:
    when: always
    reports:
      junit: report.xml
```

### Java examples

There are a few tools that can produce JUnit report format XML file in Java.

#### Gradle

In the following example, `gradle` is used to generate the test reports.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
138
If there are multiple test tasks defined, `gradle` generates multiple
GitLab Bot's avatar
GitLab Bot включено в состав коммита
139
140
141
142
143
144
145
146
147
148
149
150
151
152
directories under `build/test-results/`. In that case, you can leverage glob
matching by defining the following path: `build/test-results/test/**/TEST-*.xml`:

```yaml
java:
  stage: test
  script:
    - gradle test
  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
153
154
In [GitLab Runner 13.0](https://gitlab.com/gitlab-org/gitlab-runner/-/issues/2620)
and later, you can use `**`.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197

#### Maven

For parsing [Surefire](https://maven.apache.org/surefire/maven-surefire-plugin/)
and [Failsafe](https://maven.apache.org/surefire/maven-failsafe-plugin/) test
reports, use the following job in `.gitlab-ci.yml`:

```yaml
java:
  stage: test
  script:
    - mvn verify
  artifacts:
    when: always
    reports:
      junit:
        - target/surefire-reports/TEST-*.xml
        - target/failsafe-reports/TEST-*.xml
```

### Python example

This example uses pytest with the `--junitxml=report.xml` flag to format the output
into the JUnit report XML format:

```yaml
pytest:
  stage: test
  script:
    - pytest --junitxml=report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml
```

### C/C++ example

There are a few tools that can produce JUnit report format XML files in C/C++.

#### GoogleTest

In the following example, `gtest` is used to generate the test reports.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
198
If there are multiple `gtest` executables created for different architectures (`x86`, `x64` or `arm`),
GitLab Bot's avatar
GitLab Bot включено в состав коммита
199
200
you are required to run each test providing a unique filename. The results
are then aggregated together.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253

```yaml
cpp:
  stage: test
  script:
    - gtest.exe --gtest_output="xml:report.xml"
  artifacts:
    when: always
    reports:
      junit: report.xml
```

#### CUnit

[CUnit](https://cunity.gitlab.io/cunit/) can be made to produce [JUnit report format XML files](https://cunity.gitlab.io/cunit/group__CI.html) automatically when run using its `CUnitCI.h` macros:

```yaml
cunit:
  stage: test
  script:
    - ./my-cunit-test
  artifacts:
    when: always
    reports:
      junit: ./my-cunit-test.xml
```

### .NET example

The [JunitXML.TestLogger](https://www.nuget.org/packages/JunitXml.TestLogger/) NuGet
package can generate test reports for .Net Framework and .Net Core applications. The following
example expects a solution in the root folder of the repository, with one or more
project files in sub-folders. One result file is produced per test project, and each file
is placed in a new artifacts folder. This example includes optional formatting arguments, which
improve the readability of test data in the test widget. A full .Net Core
[example is available](https://gitlab.com/Siphonophora/dot-net-cicd-test-logging-demo).

```yaml
## Source code and documentation are here: https://github.com/spekt/junit.testlogger/

Test:
  stage: test
  script:
    - 'dotnet test --test-adapter-path:. --logger:"junit;LogFilePath=..\artifacts\{assembly}-test-result.xml;MethodFormat=Class;FailureBodyFormat=Verbose"'
  artifacts:
    when: always
    paths:
      - ./**/*test-result.xml
    reports:
      junit:
        - ./**/*test-result.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
### JavaScript example

There are a few tools that can produce JUnit report format XML files in JavaScript.

#### Jest

The [jest-junit](https://github.com/jest-community/jest-junit) npm package can generate test reports for JavaScript applications.
In the following `.gitlab-ci.yml` example, the `javascript` job uses Jest to generate the test reports:

```yaml
javascript:
  stage: test
  script:
    - 'jest --ci --reporters=default --reporters=jest-junit'
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml
```

#### Karma

The [Karma-junit-reporter](https://github.com/karma-runner/karma-junit-reporter) npm package can generate test reports for JavaScript applications.
In the following `.gitlab-ci.yml` example, the `javascript` job uses Karma to generate the test reports:

```yaml
javascript:
  stage: test
  script:
    - karma start --reporters junit
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#### Mocha

The [JUnit Reporter for Mocha](https://github.com/michaelleeallen/mocha-junit-reporter) NPM package can generate test reports for JavaScript
applications.
In the following `.gitlab-ci.yml` example, the `javascript` job uses Mocha to generate the test reports:

```yaml
javascript:
  stage: test
  script:
    - mocha --reporter mocha-junit-reporter --reporter-options mochaFile=junit.xml
  artifacts:
    when: always
    reports:
      junit:
        - junit.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
### Flutter / Dart example

This example `.gitlab-ci.yml` file uses the [JUnit Report](https://pub.dev/packages/junitreport) package to convert the `flutter test` output into JUnit report XML format:

```yaml
test:
  stage: test
  script:
    - flutter test --machine | tojunit -o report.xml
  artifacts:
    when: always
    reports:
      junit:
        - report.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
### PHP example

This example uses [PHPUnit](https://phpunit.de/) with the `--log-junit` flag.
You can also add this option using
[XML](https://phpunit.readthedocs.io/en/stable/configuration.html#the-junit-element)
in the `phpunit.xml` configuration file.

```yaml
phpunit:
  stage: test
  script:
    - composer install
    - vendor/bin/phpunit --log-junit report.xml
  artifacts:
    when: always
    reports:
      junit: report.xml
```

GitLab Bot's avatar
GitLab Bot включено в состав коммита
345
346
347
## Viewing Unit test reports on GitLab

> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/24792) in GitLab 12.5 behind a feature flag (`junit_pipeline_view`), disabled by default.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
348
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/216478) in GitLab 13.3.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
349
350

If JUnit report format XML files are generated and uploaded as part of a pipeline, these reports
GitLab Bot's avatar
GitLab Bot включено в состав коммита
351
352
can be viewed inside the pipelines details page. The **Tests** tab on this page
displays a list of test suites and cases reported from the XML file.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
353

GitLab Bot's avatar
GitLab Bot включено в состав коммита
354
![Test Reports Widget](img/pipelines_junit_test_report_v13_10.png)
GitLab Bot's avatar
GitLab Bot включено в состав коммита
355
356
357
358
359
360

You can view all the known test suites and click on each of these to see further
details, including the cases that make up the suite.

You can also retrieve the reports via the [GitLab API](../api/pipelines.md#get-a-pipelines-test-report).

GitLab Bot's avatar
GitLab Bot включено в состав коммита
361
362
363
364
365
366
367
368
### Unit test reports parsing errors

> [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/263457) in GitLab 13.10.

If parsing JUnit report XML results in an error, an indicator is shown next to the job name. Hovering over the icon shows the parser error in a tooltip. If multiple parsing errors come from [grouped jobs](jobs/index.md#group-jobs-in-a-pipeline), GitLab shows only the first error from the group.

![Test Reports With Errors](img/pipelines_junit_test_report_with_errors_v13_10.png)

GitLab Bot's avatar
GitLab Bot включено в состав коммита
369
370
371
372
373
NOTE:
GitLab.com has a 500,000 [test case parsing limit](../user/gitlab_com/#gitlab-cicd). Self-managed administrators can manage this setting on their instance.

GitLab does not parse very [large nodes](https://nokogiri.org/tutorials/parsing_an_html_xml_document.html#parse-options) of JUnit reports. There is [an issue](https://gitlab.com/gitlab-org/gitlab/-/issues/268035) open to make this optional.

GitLab Bot's avatar
GitLab Bot включено в состав коммита
374
375
## Viewing JUnit screenshots on GitLab

GitLab Bot's avatar
GitLab Bot включено в состав коммита
376
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/202114) in GitLab 13.0 behind the `:junit_pipeline_screenshots_view` feature flag, disabled by default.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
377
> - [Feature flag removed](https://gitlab.com/gitlab-org/gitlab/-/issues/216979) in GitLab 13.12.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
378

GitLab Bot's avatar
GitLab Bot включено в состав коммита
379
Upload your screenshots as [artifacts](yaml/artifacts_reports.md#artifactsreportsjunit) to GitLab. If JUnit
GitLab Bot's avatar
GitLab Bot включено в состав коммита
380
report format XML files contain an `attachment` tag, GitLab parses the attachment. Note that:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
381

GitLab Bot's avatar
GitLab Bot включено в состав коммита
382
- The `attachment` tag **must** contain the relative path to `$CI_PROJECT_DIR` of the screenshots you uploaded. For
GitLab Bot's avatar
GitLab Bot включено в состав коммита
383
  example:
GitLab Bot's avatar
GitLab Bot включено в состав коммита
384

GitLab Bot's avatar
GitLab Bot включено в состав коммита
385
386
  ```xml
  <testcase time="1.00" name="Test">
GitLab Bot's avatar
GitLab Bot включено в состав коммита
387
    <system-out>[[ATTACHMENT|/path/to/some/file]]</system-out>
GitLab Bot's avatar
GitLab Bot включено в состав коммита
388
389
  </testcase>
  ```
GitLab Bot's avatar
GitLab Bot включено в состав коммита
390

GitLab Bot's avatar
GitLab Bot включено в состав коммита
391
- You should set the job that uploads the screenshot to
GitLab Bot's avatar
GitLab Bot включено в состав коммита
392
  [`artifacts:when: always`](yaml/index.md#artifactswhen) so that it still uploads a screenshot
GitLab Bot's avatar
GitLab Bot включено в состав коммита
393
  when a test fails.
GitLab Bot's avatar
GitLab Bot включено в состав коммита
394

GitLab Bot's avatar
GitLab Bot включено в состав коммита
395
396
A link to the test case attachment appears in the test case details in
[the pipeline test report](#viewing-unit-test-reports-on-gitlab).