TestRelic

Examples

Realistic testrelic-pytest snippets — a plain unit suite, an auto-classified httpx API test, per-stage run-types, and a GitHub Actions step.

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.

tests/test_math.py
def test_addition():
    assert 1 + 1 == 2


def test_subtraction():
    assert 5 - 2 == 3
Terminal
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.

tests/test_api.py
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:

Smoke stage
pytest --testrelic-pytest-run-type smoke
Nightly stage
pytest --testrelic-pytest-run-type nightly

GitHub Actions step

.github/workflows/tests.yml
- 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.

Was this page helpful?

On this page