---
title: Pytest Integration with CI Insights
description: Report your test results from pytest to CI Insights
---

import pytestLogo from "../../../images/ci-insights/pytest/logo.svg"
import IntegrationLogo from "../../../../components/IntegrationLogo.astro"
import CIInsightsSetupNote from "../../../../components/CIInsightsSetupNote.astro"

<IntegrationLogo src={pytestLogo} alt="Pytest logo" />

This guide explains how to integrate Pytest with CI Insights using the
`pytest-mergify` plugin. Once installed, test results are automatically
uploaded to CI Insights without any extra workflow changes.

## Installation

You need to install the
[`pytest-mergify`](https://pypi.python.org/pypi/pytest-mergify) plugin to
automatically upload your test results to **CI Insights**. This can be done in
different ways depending on your Python dependency setup. Below are a few
examples:

### Pip / Requirements File

```bash
pip install pytest-mergify
```

Or add `pytest-mergify` to your `requirements.txt`.

### `setup.py`

```python
setup(
    name="your-package",
    ...
    install_requires=[
        ...
    ],
    extras_require={
        "dev": ["pytest-mergify"]
    },
    ...
)
```

Make sure those dependencies are installed when running your tests.

### `setup.cfg`

```ini
[options.extras_require]
dev =
    pytest-mergify
```

Make sure those dependencies are installed when running your tests.

### Poetry

```bash
poetry add --group dev pytest-mergify
```

## Modify Your Workflow

<CIInsightsSetupNote />

Your workflow should run your tests as usual while exporting the secret
`MERGIFY_TOKEN` as an environment variable. You’ll need to add the following
code to the GitHub Actions step running your tests:

```yaml
env:
  MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
```

For example:

```yaml
- name: Run Tests 🧪
  env:
    MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
  run: pytest
```

The plugin collects your test results and sends them to CI Insights.

Check the CI Insights dashboard afterward to view execution metrics, detect
flaky tests, and gather actionable feedback.

## Using with Tox

If you’re using [Tox](https://tox.wiki/) to manage test environments, you can
still use `pytest-mergify` by passing the `MERGIFY_TOKEN` and the rest of the
CI environment variable into the test environment.

In your GitHub Actions workflow:

```yaml
- name: Run Tox Tests
  env:
    MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
  run: tox
```

In your `tox.ini`, make sure the plugin is included in your testenv dependencies:

```ini
[testenv]
# You need to pass the MERGIFY_*, CI, GITHUB_*, etc variables
passenv = *
deps =
    pytest
    pytest-mergify
commands = pytest
```

If you’re using multiple environments (e.g. `py38`, `py39`, etc.), the plugin
will work for all of them as long as the token is set correctly.

If you’re running multiple Tox environments (e.g., py38, py39, etc.), we
recommend setting the `MERGIFY_JOB_NAME` environment variable to identify each
environment’s report in CI Insights:

In your GitHub Actions workflow:

```yaml
- name: Run Tox Tests
  env:
    MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
    MERGIFY_JOB_NAME: tox-${{ matrix.python-version }}
  run: tox
```

:::tip
Use `MERGIFY_JOB_NAME` to make reports clearer in CI Insights, especially when
running multiple Tox environments or using a matrix.
:::
