Bazel rules for the Pytest Python test framework.
current_py_pytest_toolchain(name)
A rule for exposing the current registered py_pytest_toolchain
.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required |
py_pytest_toolchain(name, pytest)
A toolchain for the pytest formatter rules.
ATTRIBUTES
Name | Description | Type | Mandatory | Default |
---|---|---|---|---|
name | A unique name for this target. | Name | required | |
pytest | The pytest py_library to use with the rules. |
Label | required |
py_pytest_test(name, srcs, coverage_rc, pytest_config, numprocesses, tags, kwargs)
A rule which runs python tests using pytest as the py_test test runner.
This rule also supports a build setting for globally applying extra flags to test invocations.
Users can add something similar to the following to their .bazelrc
files:
build --//python/pytest:extra_args=--color=yes,-vv
The example above will add --colors=yes
and -vv
arguments to the end of the pytest invocation.
Tips:
- It's common for tests to have some utility code that does not live in a test source file.
To account for this. A
py_library
can be created that contains only these sources which are then passed topy_pytest_test
viadeps
.
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_pytest//python/pytest:defs.bzl", "PYTEST_TARGET", "py_pytest_test")
py_library(
name = "test_utils",
srcs = [
"tests/__init__.py",
"tests/conftest.py",
],
deps = [PYTEST_TARGET],
testonly = True,
)
py_pytest_test(
name = "test",
srcs = ["tests/example_test.py"],
deps = [":test_utils"],
)
PARAMETERS
Name | Description | Default Value |
---|---|---|
name | The name for the current target. | none |
srcs | An explicit list of source files to test. | none |
coverage_rc | The pytest-cov configuration file to use | Label("@rules_pytest//python/pytest:coverage_rc") |
pytest_config | The pytest configuration file to use | Label("@rules_pytest//python/pytest:config") |
numprocesses | If set the pytest-xdist argument --numprocesses (-n ) will be passed to the test. |
None |
tags | Tags to set on the underlying py_test target. |
[] |
kwargs | Keyword arguments to forward to the underlying py_test target. |
none |
py_pytest_test_suite(name, tests, args, data, coverage_rc, pytest_config, kwargs)
Generates a test_suite which groups various test targets for a set of python sources.
Given an idiomatic python project structure:
BUILD.bazel
my_lib/
__init__.py
mod_a.py
mod_b.py
mod_c.py
requirements.in
requirements.txt
tests/
__init__.py
fixtures.py
test_mod_a.py
test_mod_b.py
test_mod_c.py
This rule can be used to easily define test targets:
load("@rules_python//python:defs.bzl", "py_library")
load("@rules_pytest//python/pytest:defs.bzl", "py_pytest_test_suite")
py_library(
name = "my_lib",
srcs = glob(["my_lib/**/*.py"])
imports = ["."],
)
py_pytest_test_suite(
name = "my_lib_test_suite",
# Source files containing test helpers should go here.
# Note that the test sources are excluded. This avoids
# a test to be updated without invalidating all other
# targets.
srcs = glob(
include = ["tests/**/*.py"],
exclude = ["tests/**/*_test.py"],
),
# Any data files the tests may need would be passed here
data = glob(["tests/**/*.json"]),
# This field is used for dedicated test files.
tests = glob(["tests/**/*_test.py"]),
deps = [
":my_lib",
],
)
For each file passed to tests
, a py_pytest_test target will be created. From the example above,
the user should expect to see the following test targets:
//:my_lib_test_suite
//:tests/test_mod_a
//:tests/test_mod_b
//:tests/test_mod_c
Additional Notes:
- No file passed to
tests
should be passed found in thesrcs
ordata
attributes or tests will not be able to be individually cached.
PARAMETERS