Skip to main content

pytest-bazel

standards

Documentation Changelog Contributing

Bazel test encyclopedia support

How pytest_bazel follows the test encyclopedia when running under bazel test.

Bazel pytest_bazel Docs Reference
TESTBRIDGE_TEST_ONLY (--test_filter) Selects tests with a -k expression, e.g. TestClass.test_fn, a pytest node id, or several of them separated by commas. Example Initial conditions
TESTBRIDGE_TEST_RUNNER_FAIL_FAST (--test_runner_fail_fast) Passes --exitfirst. Example Flag
TEST_INFRASTRUCTURE_FAILURE_FILE Written when pytest itself fails, i.e. on a usage or internal error, so that a broken setup can be told apart from failing tests. Example Initial conditions
TEST_LOGSPLITTER_OUTPUT_FILE Not supported. Not supported Initial conditions
TEST_PREMATURE_EXIT_FILE Created before pytest runs and removed once it returns, so tests that exit early fail. Example Initial conditions
TEST_RANDOM_SEED, TEST_RUN_NUMBER (--runs_per_test) Seeds pytest-randomly with --randomly-seed, or random.seed without it. Example Initial conditions
TEST_SHARD_INDEX, TEST_TOTAL_SHARDS, TEST_SHARD_STATUS_FILE (shard_count) Built-in sharding: balanced shards, empty shards pass. Example Test sharding
TEST_SIZE Not supported. Not supported Initial conditions
TEST_TARGET Names the JUnit test suite, and selects the package for the opt-in flags below. Example Initial conditions
TEST_TIMEOUT (timeout, --test_timeout) On timeout bazel sends SIGTERM: the stacks of all threads are printed and pytest is interrupted, so it still reports where the tests hung and writes the JUnit XML. Example Initial conditions
TEST_TMPDIR Used for --basetemp and for TMPDIR, TMP and TEMP, through a short alias when the path is too long for AF_UNIX sockets. Example Filesystem
TEST_UNDECLARED_OUTPUTS_ANNOTATIONS_DIR A summary of the results and the failed tests is written as an annotation, which bazel adds to bazel-testlogs/<target>/test.outputs_manifest/ANNOTATIONS. Example Filesystem
TEST_UNDECLARED_OUTPUTS_DIR Relative report paths of --html, --json-report-file and --report-log are written there. With PYTEST_BAZEL_KEEP_FAILED_TMP_PATH=1, so is the tmp_path of failed tests. Example Filesystem
TEST_WARNINGS_OUTPUT_FILE Python warnings are written to it. Under bazel test, here and in pytest's warnings summary, the paths are relative to the runfiles root, i.e. the workspace root, so editors can open them. Example Initial conditions
XML_OUTPUT_FILE Passes --junitxml. Example Initial conditions

See the features in the docs for each of them in action.

Not supported

These parts of the test encyclopedia are out of scope for pytest_bazel:

  • TEST_LOGSPLITTER_OUTPUT_FILE: a Google internal protobuf log format that nothing outside of Google reads.
  • TEST_SIZE: bazel already uses the size for the timeout and the resources of a test, there is nothing left for the test runner to do.
  • TEST_SRCDIR: deprecated. Tests run in the runfiles root, use the runfiles library of your rules to find files.
  • TEST_WORKSPACE: it always names the main repository, so it does not help with tests from other repositories.
  • Coverage (COVERAGE_*): collected by the Python rules, e.g. the py_test of rules_python, which wraps the whole test process.

pytest plugins

Plugin pytest_bazel Docs
pytest-randomly Seeded from TEST_RANDOM_SEED with --runs_per_test. Example
pytest-shard Not needed, sharding is built in. Example
pytest-xdist Works with tags = ["cpu:N"], prints the stacks of the workers on timeout. Example

Guides

Thanks

Special thanks to rules_python_pytest and rules_py projects that had some great ideas how to integrate with pytest. This attempts to unify all of the approaches.

Planned for 1.0

Behaviour changes are added behind opt-in flags first. Their defaults will be flipped in 1.0. Set them in the env of a test, or for all tests with e.g. test --test_env=PYTEST_BAZEL_SCOPE_COLLECTION_TO_PACKAGE=1 in .bazelrc.

Env var Default Default in 1.0 Description Docs
PYTEST_BAZEL_CONFIG_FROM_PACKAGE 0 1 Use the pytest config file (e.g. pytest.ini) in the bazel package of the target or its parents. pytest only looks in the runfiles root. Example
PYTEST_BAZEL_CONSIDER_NAMESPACE_PACKAGES 0 1 Pass -o consider_namespace_packages=true on pytest 8.1+, so that test modules in packages without __init__.py do not shadow third-party packages. Example
PYTEST_BAZEL_JUNIT_OUTPUT 0 1 Add the stdout, stderr and logs of failed tests to the JUnit XML, which CI shows. Example
PYTEST_BAZEL_KEEP_FAILED_TMP_PATH 0 1 Keep the tmp_path of failed tests in TEST_UNDECLARED_OUTPUTS_DIR, i.e. in bazel-testlogs/<target>/test.outputs. Needs pytest 7.3+. Example
PYTEST_BAZEL_SCOPE_COLLECTION_TO_PACKAGE 0 1 Only collect tests from the bazel package of the target when no paths are passed. Example

Metadata

Release files for pytest-bazel 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for pytest-bazel 0.4.0
File Interpreter ABI Platform Actions
pytest_bazel-0.4.0-py3-none-any.whl Python 3 none any Details for pytest_bazel-0.4.0-py3-none-any.whl

Release files / pytest_bazel-0.4.0-py3-none-any.whl

Download URL pytest_bazel-0.4.0-py3-none-any.whl
Size 26.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
798c2b9a246c0dbf86e06af45df08219869bac3d4b4b6d31972338aa28a47494
BLAKE2b-256 checksum
How to use checksums
4a9190fef5a66f6140081d01f9c14c1de961e429378a80d9115a3608ce6e0df1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

Security for pytest-bazel 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

1 file in this release has been uploaded with provenance metadata.

Attestation for pytest_bazel-0.4.0-py3-none-any.whl

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 15, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page