Examples
These snippets show testrelic-pytest in common situations. In each case the plugin is already installed, so there is nothing to import.
Plain unit-test suite
A normal pytest suite needs no changes — install the package, set your key, and run.
def test_addition():
assert 1 + 1 == 2
def test_subtraction():
assert 5 - 2 == 3
export TESTRELIC_API_KEY=tr_live_...
pytest
These are reported as standard tests.
httpx API test (auto-classified)
A test that makes an httpx call is automatically classified as a rest API test and opens the API Session Workspace.
import httpx
def test_create_user():
response = httpx.post(
"https://api.example.com/users",
json={"name": "Ada"},
)
assert response.status_code == 201
No marker or decorator is needed — the HTTP call is detected automatically.
Per-stage run-type
Use --testrelic-pytest-run-type to bucket runs by CI stage on the dashboard:
pytest --testrelic-pytest-run-type smoke
pytest --testrelic-pytest-run-type nightly
GitHub Actions step
- name: Run tests with TestRelic
env:
TESTRELIC_API_KEY: ${{ secrets.TESTRELIC_API_KEY }}
run: |
pip install testrelic-pytest
pytest --testrelic-pytest-run-type ci
CI metadata (GitHub Actions, GitLab CI, Jenkins, CircleCI) and git branch/commit/author are detected automatically. See GitHub Actions integration for more.
Running across multiple workers with pytest -n 4 is supported — only the controller uploads results.