Enhancing Merge Requests with Vedro and GitLab Test Reports
Published: 2024-01-06 09:59:51

As more development teams adopt Continuous Integration (CI) practices, the ability to visualize and manage test results within merge requests becomes increasingly important. GitLab CI provides developers with a user-friendly platform to view [test reports](https://docs.gitlab.com/ee/ci/testing/unit_test_reports.html) directly in the context of their changes, streamlining the workflow and speeding up the review process.

When configured correctly, your GitLab merge request will display a new section illustrating the test report, showcasing which tests passed or failed. This section makes it incredibly easy for reviewers to assess the impact of code changes on the existing codebase, all without leaving the merge request view. The integration appears as follows:

![overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kboxmm0g1mb7jqi97me7.png)

_Full Report:_

![fullreport](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0dq4s0dv05343ea6r2a.png)

## Setting Up GitLab CI Test Reports

### Step 1: Installing vedro-xunit-reporter

Before generating test reports, you need to install the [vedro-xunit-reporter](https://pypi.org/project/vedro-xunit-reporter/) plugin. This plugin will produce test reports in the xUnit XML format, which GitLab CI can interpret and display within merge requests.

To install the plugin, run the following command:

```sh
$ vedro plugin install vedro-xunit-reporter
```

After installing the plugin, remember to add it to your `requirements.txt` file, ensuring its installation during CI pipeline executions.

### Step 2: Configuring .gitlab-ci.yml

The next step involves updating your `.gitlab-ci.yml` file to define how artifacts and test reports are handled within your GitLab CI pipeline. You will need to specify that the artifact produced by the test stage is a report in the xUnit XML format, and indicate the path where this report can be found.

Here is an example configuration for your `.gitlab-ci.yml` file:

```yml
test:
  image: python:3.11
  stage: test

  before_script:
    - pip install -r requirements.txt

  script:
    - vedro run -r rich xunit --xunit-report-path xunit_report.xml

  artifacts:
    when: always
    reports:
      junit: xunit_report.xml
```

In the example above, the CI pipeline includes a test stage, running Vedro with the [rich](https://vedro.io/docs/basics/reporting-system) and [xunit](https://pypi.org/project/vedro-xunit-reporter/) reporters. The `--xunit-report-path` option specifies the output filename for the test report, which, in this case, is `xunit_report.xml`.

Additionally, the `artifacts:reports:junit` section instructs GitLab CI to treat the generated `xunit_report.xml` file as a JUnit format test report. `when: always` ensures that the report is saved as an artifact regardless of whether the test passes or fails.

By following these two steps, you can initiate [Vedro](https://vedro.io/) tests within your GitLab CI pipeline and visually represent the results in merge requests. This integration enhances the efficiency of code reviews by providing immediate and clear feedback on the success of tests related to the proposed code changes. It also helps to catch issues early, prior to merging your code, thus maintaining the stability and reliability of your code base.