Pytest TestRail Plugin
pytest-pytestrail is a Pytest plugin designed to facilitate interaction with TestRail, a web-based test case management tool. It allows users to automatically update TestRail test results directly from their Pytest runs. The current version is 0.10.5, and it typically sees minor updates for compatibility and feature enhancements.
Common errors
-
Failed to connect to TestRail (HTTP 401: Authorization Required)
cause Incorrect TestRail email or password, or an invalid API key for a TestRail instance configured to use API keys instead of passwords.fixVerify `tr-email` and `tr-password` (or `tr-api-key`) in `pytest.ini` or command-line arguments. Ensure the user has permissions to access the specified TestRail project. -
pytest: error: unrecognized arguments: --testrail
cause The `pytest-pytestrail` plugin is either not installed, or pytest cannot find it in your environment.fixRun `pip install pytest-pytestrail` to ensure the plugin is installed in your active Python environment. If using a virtual environment, ensure it's activated. -
pytest.mark.testrail must receive a string or list of strings as arguments for case IDs.
cause Non-string arguments (e.g., integers, booleans) were passed to the `@pytest.mark.testrail()` decorator.fixEnsure all TestRail case IDs passed to the decorator are strings, e.g., `@pytest.mark.testrail('C1234')` or `@pytest.mark.testrail(['C1234', 'C5678'])`.
Warnings
- gotcha TestRail credentials (URL, email, password) must be correctly configured. Incorrect details will lead to 'Failed to connect' or 'Authorization Required' errors. It is highly recommended to use environment variables for passwords.
- gotcha The plugin will not activate without the `--testrail` command-line option or `addopts = --testrail` in `pytest.ini`. Without it, tests will run but results will not be pushed to TestRail.
- gotcha TestRail case IDs used with `@pytest.mark.testrail()` must be valid strings (e.g., 'C1234') and must exist in your specified TestRail project. Invalid or non-existent IDs will result in errors during reporting.
Install
-
pip install pytest-pytestrail
Quickstart
# pytest.ini
[pytest]
addopts = --testrail
tr-url = https://your.testrail.net/
tr-email = your_email@example.com
tr-password = ${TR_PASSWORD}
tr-project-name = My Project
tr-testrun-name = Automated Test Run
# test_example.py
import pytest
import os
# For quickstart, ensure TR_PASSWORD is set in environment, e.g., via export TR_PASSWORD='...'
# Or replace ${TR_PASSWORD} with actual password for local testing only (NOT recommended for production)
@pytest.mark.testrail('C1234')
def test_feature_a_works():
assert True
@pytest.mark.testrail('C5678')
def test_feature_b_fails():
assert False # This will be reported as failed in TestRail