Pytest
The Orangebeard pytest plugin reports pytest test runs, including tests written with pytest-playwright (Playwright for Python).
Reporting problems never fail your tests, and without an Orangebeard configuration the plugin does nothing.
Using Playwright with JavaScript or TypeScript? Follow the Playwright guide instead.
Installation
The plugin requires Python 3.10 or newer and pytest 7.4 or newer. Install the pip package:
pip install orangebeard-pytest
pytest picks the plugin up automatically; you do not have to register it.
The source code can be found on GitHub: Pytest Listener.
Configuration
Create a file named orangebeard.json in your project's root directory (or in any parent directory):
{
"endpoint": "https://app.orangebeard.io/{ORGANIZATION}",
"token": "{LISTENER_ACCESS_TOKEN}",
"project": "{ORANGEBEARD_PROJECT}",
"testset": "pytest Tests",
"description": "A run from pytest",
"attributes": [
{
"key": "env",
"value": "acceptance"
},
{
"value": "smoke"
}
],
"referenceUrl": "https://link.to.my/build-server"
}
endpoint, token and project are required. Reporting is active as soon as all three are known. Without a
testset, the name of the pytest root directory is used.
The same settings can be given as environment variables, with or without an orangebeard.json file. Keep static
settings such as endpoint and project in the file, and provide secrets (the token) and values that change per run
through the environment:
export ORANGEBEARD_ENDPOINT="https://app.orangebeard.io/{ORGANIZATION}"
export ORANGEBEARD_TOKEN="{LISTENER_ACCESS_TOKEN}"
export ORANGEBEARD_PROJECT="{ORANGEBEARD_PROJECT}"
export ORANGEBEARD_TESTSET="pytest Tests"
export ORANGEBEARD_DESCRIPTION="A run from pytest"
export ORANGEBEARD_ATTRIBUTES="env:acceptance;team:web"
export ORANGEBEARD_REFERENCE_URL="https://link.to.my/build-server"
Settings can also be set in your pytest configuration file or on the command line:
| Setting | Command line | pytest configuration file |
|---|---|---|
| Endpoint | --ob-endpoint | |
| Project | --ob-project | orangebeard_project |
| Test set | --ob-testset | orangebeard_testset |
| Description | --ob-description | orangebeard_description |
| Attributes | --ob-attribute key:value (repeatable) |
Later steps override earlier ones:
orangebeard.jsonfile- Environment variables
- pytest configuration file (
pytest.ini,pyproject.toml,tox.iniorsetup.cfg) - Command-line options
Appending attributes: Attributes from orangebeard.json, ORANGEBEARD_ATTRIBUTES and --ob-attribute are
combined. An assignee attribute auto-assigns the test run.
Plugin options
These options control what the plugin reports. Set them in your pytest configuration file:
| Option | Default | Meaning |
|---|---|---|
orangebeard_log_level | INFO | Minimum logging level that is reported |
orangebeard_report_fixtures | user | Which fixtures are reported as steps: user (your own), all or none |
orangebeard_max_attachment_mb | 50 | Larger attachments are skipped, with a warning in the test log |
orangebeard_watch_dirs | Extra directories; files a test creates there are attached to it | |
orangebeard_log_parameters | true | Send the values of test parameters |
[pytest]
orangebeard_testset = Web UI tests
orangebeard_log_level = DEBUG
orangebeard_watch_dirs =
reports
downloads
Usage
Run your tests as usual:
pytest
When the run has finished, the terminal summary shows where the results are in Orangebeard.
| Command-line option | Meaning |
|---|---|
--orangebeard | Stop with a clear error when the configuration is incomplete |
--ob-no-report | Do not report this run, even when Orangebeard is configured |
Using --orangebeard in a pipeline prevents a run from silently going unreported, for example when the access token is
missing.
Parallel execution
Reporting parallel runs requires orangebeard-pytest 0.4.0 or newer.
One runner: pytest-xdist
Running tests in parallel with pytest-xdist needs no extra configuration:
pytest -n 4
The plugin starts one test run before the workers start, every worker reports its tests into it, and the run is finished when all workers are done.
With more than one worker, the output directories are shared, so a new file cannot be traced to a test. Only the files
in a test's own pytest-playwright folder (screenshots, traces and videos) are attached automatically;
orangebeard_watch_dirs is ignored. Attach other files with ob.attach().
Several runners: CI jobs
When separate jobs, such as shards or a job per browser, should report into one test run, start and finish the run
outside pytest with orangebeard-cli. It is installed together with the plugin.
# Once, before the test jobs
testrunUuid=$(orangebeard-cli -x start -s "pytest Tests")
# In every test job
ORANGEBEARD_TESTRUN_UUID=$testrunUuid pytest tests/shard1
# Once, after all test jobs, also when tests failed
orangebeard-cli -x finish -id "$testrunUuid"
The command line utility reads the endpoint, access token and project from orangebeard.json or the environment.
When your CI system runs these steps as separate jobs, pass the UUID from the first job to the others, for example as
an output variable or artifact.
- With
ORANGEBEARD_TESTRUN_UUIDset, the plugin reports into that run and neither starts nor finishes it. The test set, description and run attributes are the ones given toorangebeard-cli -x start. - The test jobs can use
pytest -nas well. - Tests need different names in different jobs. When two jobs run the same test, for example on two Python versions, Orangebeard sees the second result as a retry. The pytest-playwright browser is part of the test name.
How tests are reported
Directories, modules and classes become nested suites. Every test has a setup, call and teardown step, and your
fixtures are shown as sub steps:
suite tests (directory)
└ suite test_cart.py (module)
└ suite TestCart (class)
└ test test_checkout[chromium]
├ step setup
│ └ step fixture: my_database
├ step call
│ └ step Add item to cart
│ └ step Verify badge
└ step teardown
- A test fails on a setup error, a test failure, a teardown error or a strict
xpass. - Skipped and
xfailtests are reported as skipped, with their reason. - The docstring of a test becomes its description.
- Your own pytest markers become attributes of the test.
Logs, steps and attachments
The plugin reports logging records, captured stdout and stderr, and failure details without any changes to your
tests.
To add your own nested steps, logs and attachments, use the orangebeard_pytest module:
import orangebeard_pytest as ob
def test_checkout(page):
with ob.step("Add item to cart"):
page.click("#add")
with ob.step("Verify badge"):
assert page.inner_text("#badge") == "1"
ob.log("cart looks fine", level="info")
ob.attach("reports/summary.html") # a file path
ob.attach(b'{"items": 1}', name="cart.json") # bytes need a name
@ob.step("Log in") # also works as a decorator
def login(page):
...
The same functions are available through the orangebeard fixture. When Orangebeard is not configured they do
nothing, so your tests keep working.
Markdown in logs
Log messages containing Markdown, such as tables, **bold** text, ## headings, code blocks or links, are shown
formatted in Orangebeard. All other messages are shown as plain text. Tracebacks are always shown as a code block.
In Markdown messages the plugin escapes < outside code, so that text such as <Locator ...> stays visible. HTML
such as <br> is therefore shown as text.
Parametrized tests
Orangebeard treats tests with the same name as retries of one test, and a name that changes with the test data breaks a test's history. Test names therefore use the ids you give, never the test data itself:
| Parametrization | Reported as |
|---|---|
ids=["admin", "guest"] or pytest.param(..., id="admin") | test_login[admin], test_login[guest] |
| No ids, several cases | test_login - iteration #1, test_login - iteration #2 |
| Duplicate ids | test_login[admin] - iteration #1, ... #2 |
| No ids, a single case | test_login |
See the pytest documentation for the ways to set ids. Ids longer than 100 characters are treated as data and are not used in the name.
The parameter values are logged with the test and added to its description.
Parameter values are sent as they are, including passwords or other secrets you pass as a parameter. Set
orangebeard_log_parameters = false to send only the pytest ids.
Playwright
With pytest-playwright there is nothing extra to configure. The screenshots, traces and videos it writes while a test runs are attached to that test. Use the pytest-playwright options to choose what is recorded:
pytest --screenshot only-on-failure --tracing retain-on-failure --video retain-on-failure
The browser is part of the test name, for example test_checkout[chromium], so results per browser keep their own
history. Open a downloaded trace with playwright show-trace trace.zip.
Limitations
- Fixture finalizers are not reported as separate steps; their output is part of the
teardownstep.
Ask Orangebeard