Pytest Integration with Mergify
Report your test results from pytest to Mergify
This guide explains how to integrate Pytest with Test Insights using the
pytest-mergify plugin. Once installed, test results are automatically
uploaded to Test Insights without any extra workflow changes.
Installation
Section titled InstallationYou need to install the
pytest-mergify plugin to
automatically upload your test results to Test Insights. This can be done in
different ways depending on your Python dependency setup. Below are a few
examples:
Pip / Requirements File
Section titled Pip / Requirements Filepip install pytest-mergifyOr add pytest-mergify to your requirements.txt.
setup.py
Section titled setup.pysetup( name="your-package", ... install_requires=[ ... ], extras_require={ "dev": ["pytest-mergify"] }, ...)Make sure those dependencies are installed when running your tests.
setup.cfg
Section titled setup.cfg[options.extras_require]dev = pytest-mergifyMake sure those dependencies are installed when running your tests.
Poetry
Section titled Poetrypoetry add --group dev pytest-mergifyUpdate Your CI Workflow
Section titled Update Your CI WorkflowYour workflow should run your tests as usual while exporting the secret
MERGIFY_TOKEN as an environment variable.
GitHub Actions
Section titled GitHub ActionsAdd the following to the GitHub Actions step running your tests:
env: MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}For example:
- name: Run Tests 🧪 env: MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }} run: pytestBuildkite
Section titled BuildkiteSet MERGIFY_TOKEN in the environment of the agents running your tests.
The step itself then needs no Mergify-specific configuration:
steps: - label: "Run Tests 🧪" command: pytestThe plugin collects your test results and sends them to Test Insights.
Using with Tox
Section titled Using with ToxIf you’re using Tox to manage test environments, you can
still use pytest-mergify by passing the MERGIFY_TOKEN and the rest of the
CI environment variables into the test environment. On Buildkite the token comes
from the agent’s environment, as above, so only the GitHub Actions
step names it.
In your CI workflow:
# GitHub Actions- name: Run Tox Tests env: MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }} run: tox# Buildkitesteps: - label: "Run Tox Tests" command: toxIn your tox.ini, make sure the plugin is included in your testenv dependencies:
[testenv]# You need to pass the MERGIFY_*, CI, GITHUB_*, etc variablespassenv = *deps = pytest pytest-mergifycommands = pytestIf 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_TEST_JOB_NAME environment variable to identify each
environment’s report in Test Insights:
In your CI workflow:
# GitHub Actions- name: Run Tox Tests env: MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }} MERGIFY_TEST_JOB_NAME: tox-${{ matrix.python-version }} run: tox# Buildkitesteps: - label: "Run Tox Tests ({{matrix}})" command: tox matrix: - "3.10" - "3.11" - "3.12" env: MERGIFY_TEST_JOB_NAME: "tox-{{matrix}}"Verify and Review in Test Insights
Section titled Verify and Review in Test InsightsAfter pushing these changes, your next CI run reports its pytest results automatically.
You can then review your test results, including any failures or flaky tests, directly in the Test Insights dashboard.
Was this page helpful?
Thanks for your feedback!