Skip to content

Commit 8e7d43f

Browse files
committed
also set cwd
1 parent d2595df commit 8e7d43f

3 files changed

Lines changed: 18 additions & 26 deletions

File tree

examples/pytest/BUILD.bazel

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,24 @@
1-
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_pytest_main", "py_test")
1+
load("@aspect_rules_py//py:defs.bzl", "py_library", "py_test")
22

33
py_library(
44
name = "lib",
55
srcs = ["foo.py"],
66
imports = ["../.."],
77
)
88

9-
py_pytest_main(
10-
name = "__test__",
11-
chdir = package_name(), # So that test fixtures are available at the correct path
12-
deps = [
13-
"@pypi_coverage//:pkg",
14-
"@pypi_pytest//:pkg",
15-
],
16-
)
17-
189
py_test(
1910
name = "pytest_test",
20-
srcs = [
21-
"foo_test.py",
22-
":__test__",
23-
],
11+
srcs = ["foo_test.py"],
2412
data = glob([
2513
"fixtures/*.json",
2614
]),
2715
env_inherit = ["FOO"],
2816
imports = ["../.."],
29-
main = ":__test__.py",
3017
package_collisions = "warning",
18+
pytest_main = True,
3119
deps = [
32-
":__test__",
3320
":lib",
21+
"@pypi_coverage//:pkg",
3422
"@pypi_ftfy//:pkg",
3523
"@pypi_neptune//:pkg",
3624
"@pypi_pytest//:pkg",
@@ -40,15 +28,10 @@ py_test(
4028
py_test(
4129
# NB: name contains a slash as regression test for #483
4230
name = "sharded/test",
43-
srcs = [
44-
"__test__.py",
45-
"sharding_test.py",
46-
],
31+
srcs = ["sharding_test.py"],
4732
imports = ["../.."],
48-
main = "__test__.py",
4933
package_collisions = "warning",
34+
pytest_main = True,
5035
shard_count = 2,
51-
deps = [
52-
"__test__",
53-
],
36+
deps = ["@pypi_pytest//:pkg"],
5437
)

py/defs.bzl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,11 @@ def py_test(name, srcs = [], main = None, pytest_main = False, **kwargs):
147147
fail("When pytest_main is set, the main attribute should not be set.")
148148
pytest_main_target = name + ".pytest_main"
149149
main = pytest_main_target + ".py"
150-
py_pytest_main(name = pytest_main_target)
150+
py_pytest_main(
151+
name = pytest_main_target,
152+
# Ensure that test fixtures are available at the expected path
153+
chdir = native.package_name(),
154+
)
151155
srcs.append(main)
152156
deps.append(pytest_main_target)
153157

py/private/pytest.py.tmpl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import os
1717
from pathlib import Path
1818
from typing import List
1919

20-
import pytest
20+
try:
21+
import pytest
22+
except ModuleNotFoundError as e:
23+
print("ERROR: pytest must be included in the deps of the py_test target")
24+
raise e
25+
2126
# None means coverage wasn't enabled
2227
cov = None
2328

0 commit comments

Comments
 (0)