Skip to content

Commit

Permalink
feat: py_test macro generates a py_pytest_main
Browse files Browse the repository at this point in the history
Provides much-needed syntax sugar for this common case.
  • Loading branch information
alexeagle committed Jan 28, 2025
1 parent eb45eca commit e3c094a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 43 deletions.
24 changes: 6 additions & 18 deletions e2e/use_release/src/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_pytest_main", "py_test")
load("@aspect_rules_py//py:defs.bzl", "py_binary", "py_test")

py_binary(
name = "main",
Expand All @@ -9,24 +9,12 @@ py_binary(
main = "__main__.py",
)

# TODO(alex): land https://github.com/aspect-build/rules_py/pull/401 and shorten this
py_pytest_main(
name = "__test__",
deps = [
"@pip//coverage",
"@pip//pytest",
],
)

py_test(
name = "test",
srcs = [
"my_test.py",
":__test__",
],
main = ":__test__.py",
deps = [
":__test__",
":main",
srcs = ["my_test.py"],
pytest_main_deps = [
"@pip//coverage",
"@pip//pytest",
],
deps = [":main"],
)
24 changes: 1 addition & 23 deletions examples/pytest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,7 @@ py_test(
)

py_test(
name = "nested/pytest",
srcs = [
"foo_test.py",
":__test__",
],
data = glob([
"fixtures/*.json",
]),
env_inherit = ["FOO"],
imports = ["../.."],
main = ":__test__.py",
package_collisions = "warning",
deps = [
":__test__",
":lib",
"@pypi_ftfy//:pkg",
"@pypi_neptune//:pkg",
"@pypi_pytest//:pkg",
],
)

py_test(
name = "sharding_test",
name = "sharded/test",
srcs = [
"__test__.py",
"sharding_test.py",
Expand Down
17 changes: 15 additions & 2 deletions py/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def py_binary(name, srcs = [], main = None, **kwargs):

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

def py_test(name, srcs = [], main = None, **kwargs):
def py_test(name, srcs = [], main = None, pytest_main_deps = None, **kwargs):
"""Identical to [py_binary](./py_binary.md), but produces a target that can be used with `bazel test`.
Args:
Expand All @@ -127,6 +127,8 @@ def py_test(name, srcs = [], main = None, **kwargs):
Like rules_python, this is treated as a suffix of a file that should appear among the srcs.
If absent, then `[name].py` is tried. As a final fallback, if the srcs has a single file,
that is used as the main.
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.
The supplied labels define the deps attribute for the generated py_pytest_main.
**kwargs: additional named parameters to `py_binary_rule`.
"""

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

_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, main = main, resolutions = resolutions, **kwargs)
deps = kwargs.pop("deps", [])
if pytest_main_deps:
pytest_main_target = name + ".pytest_main"
main = pytest_main_target + ".py"
py_pytest_main(
name = pytest_main_target,
deps = pytest_main_deps,
)
srcs.append(main)
deps.append(pytest_main_target)

_py_binary_or_test(name = name, rule = _py_test, srcs = srcs, deps = deps, main = main, resolutions = resolutions, **kwargs)

0 comments on commit e3c094a

Please sign in to comment.