Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/py_test.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions examples/pytest/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,13 @@ py_test(
shard_count = 2,
deps = ["@pypi_pytest//:pkg"],
)

py_test(
# NB: target name starts with a test_ prefix
name = "test_prefix",
srcs = ["prefix_test.py"],
pytest_main = True,
deps = [
"@pypi_pytest//:pkg",
],
)
2 changes: 2 additions & 0 deletions examples/pytest/prefix_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
def test_always_passing():
assert True
7 changes: 5 additions & 2 deletions py/defs.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,11 @@ def py_test(name, srcs = [], main = None, pytest_main = False, **kwargs):
if main:
fail("When pytest_main is set, the main attribute should not be set.")
pytest_main_target = name + ".pytest_main"
main = pytest_main_target + ".py"
py_pytest_main(name = pytest_main_target)

# NB: The main name starts with a dot, so pytest does not mistakenly collect
# the file during test collection even if the target name starts with `test_`.
main = "." + pytest_main_target + ".py"
py_pytest_main(name = pytest_main_target, test_main = main)
srcs.append(main)
deps.append(pytest_main_target)

Expand Down
5 changes: 3 additions & 2 deletions py/private/py_pytest_main.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,20 @@ _py_pytest_main = rule(
},
)

def py_pytest_main(name, py_library = default_py_library, deps = [], data = [], testonly = True, **kwargs):
def py_pytest_main(name, test_main = None, py_library = default_py_library, deps = [], data = [], testonly = True, **kwargs):
"""py_pytest_main wraps the template rendering target and the final py_library.

Args:
name: The name of the runable target that updates the test entry file.
test_main: The name of the test entry file to be generated.
py_library: Use this attribute to override the default py_library rule.
deps: A list containing the pytest library target, e.g., @pypi_pytest//:pkg.
data: A list of data dependencies to pass to the py_library target.
testonly: A boolean indicating if the py_library target is testonly.
**kwargs: The extra arguments passed to the template rendering target.
"""

test_main = name + ".py"
test_main = test_main or name + ".py"
tags = kwargs.pop("tags", [])
visibility = kwargs.pop("visibility", [])

Expand Down