Skip to content

Commit e3c094a

Browse files
committed
feat: py_test macro generates a py_pytest_main
Provides much-needed syntax sugar for this common case.
1 parent eb45eca commit e3c094a

3 files changed

Lines changed: 22 additions & 43 deletions

File tree

e2e/use_release/src/BUILD.bazel

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_pytest_main", "py_test")
1+
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_test")
22

33
py_binary(
44
name = "main",
@@ -9,24 +9,12 @@ py_binary(
99
main = "__main__.py",
1010
)
1111

12-
# TODO(alex): land https://github.com/aspect-build/rules_py/pull/401 and shorten this
13-
py_pytest_main(
14-
name = "__test__",
15-
deps = [
16-
"@pip//coverage",
17-
"@pip//pytest",
18-
],
19-
)
20-
2112
py_test(
2213
name = "test",
23-
srcs = [
24-
"my_test.py",
25-
":__test__",
26-
],
27-
main = ":__test__.py",
28-
deps = [
29-
":__test__",
30-
":main",
14+
srcs = ["my_test.py"],
15+
pytest_main_deps = [
16+
"@pip//coverage",
17+
"@pip//pytest",
3118
],
19+
deps = [":main"],
3220
)

examples/pytest/BUILD.bazel

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,29 +38,7 @@ py_test(
3838
)
3939

4040
py_test(
41-
name = "nested/pytest",
42-
srcs = [
43-
"foo_test.py",
44-
":__test__",
45-
],
46-
data = glob([
47-
"fixtures/*.json",
48-
]),
49-
env_inherit = ["FOO"],
50-
imports = ["../.."],
51-
main = ":__test__.py",
52-
package_collisions = "warning",
53-
deps = [
54-
":__test__",
55-
":lib",
56-
"@pypi_ftfy//:pkg",
57-
"@pypi_neptune//:pkg",
58-
"@pypi_pytest//:pkg",
59-
],
60-
)
61-
62-
py_test(
63-
name = "sharding_test",
41+
name = "sharded/test",
6442
srcs = [
6543
"__test__.py",
6644
"sharding_test.py",

py/defs.bzl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def py_binary(name, srcs = [], main = None, **kwargs):
117117

118118
_py_binary_or_test(name = name, rule = _py_binary, srcs = srcs, main = main, resolutions = resolutions, **kwargs)
119119

120-
def py_test(name, srcs = [], main = None, **kwargs):
120+
def py_test(name, srcs = [], main = None, pytest_main_deps = None, **kwargs):
121121
"""Identical to [py_binary](./py_binary.md), but produces a target that can be used with `bazel test`.
122122
123123
Args:
@@ -127,6 +127,8 @@ def py_test(name, srcs = [], main = None, **kwargs):
127127
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
128128
If absent, then `[name].py` is tried. As a final fallback, if the srcs has a single file,
129129
that is used as the main.
130+
pytest_main_deps: List of labels. If set, generate a [py_pytest_main](#py_pytest_main) script and use it as the py_test entry point.
131+
The supplied labels define the deps attribute for the generated py_pytest_main.
130132
**kwargs: additional named parameters to `py_binary_rule`.
131133
"""
132134

@@ -139,4 +141,15 @@ def py_test(name, srcs = [], main = None, **kwargs):
139141
if resolutions:
140142
resolutions = resolutions.to_label_keyed_dict()
141143

142-
_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, main = main, resolutions = resolutions, **kwargs)
144+
deps = kwargs.pop("deps", [])
145+
if pytest_main_deps:
146+
pytest_main_target = name + ".pytest_main"
147+
main = pytest_main_target + ".py"
148+
py_pytest_main(
149+
name = pytest_main_target,
150+
deps = pytest_main_deps,
151+
)
152+
srcs.append(main)
153+
deps.append(pytest_main_target)
154+
155+
_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, deps = deps, main = main, resolutions = resolutions, **kwargs)

0 commit comments

Comments
 (0)