diff --git a/src/python/pants/backend/python/dependency_inference/rules_test.py b/src/python/pants/backend/python/dependency_inference/rules_test.py index 7873b4f137c..c6bddbd9149 100644 --- a/src/python/pants/backend/python/dependency_inference/rules_test.py +++ b/src/python/pants/backend/python/dependency_inference/rules_test.py @@ -159,14 +159,16 @@ def test_infer_python_conftests(self) -> None: ) self.create_file("src/python/root/conftest.py") - self.add_to_build_file("src/python/root", "python_library(sources=['conftest.py'])") + self.add_to_build_file("src/python/root", "python_library()") self.create_file("src/python/root/mid/conftest.py") - self.add_to_build_file("src/python/root/mid", "python_library(sources=['conftest.py'])") + self.add_to_build_file("src/python/root/mid", "python_library()") self.create_file("src/python/root/mid/leaf/conftest.py") self.create_file("src/python/root/mid/leaf/this_is_a_test.py") - self.add_to_build_file("src/python/root/mid/leaf", "python_tests()") + self.add_to_build_file( + "src/python/root/mid/leaf", "python_tests()\npython_library(name='conftest')" + ) def run_dep_inference(address: Address) -> InferredDependencies: target = self.request_single_product( @@ -181,6 +183,11 @@ def run_dep_inference(address: Address) -> InferredDependencies: [ Address("src/python/root", relative_file_path="conftest.py", target_name="root"), Address("src/python/root/mid", relative_file_path="conftest.py", target_name="mid"), + Address( + "src/python/root/mid/leaf", + relative_file_path="conftest.py", + target_name="conftest", + ), ], sibling_dependencies_inferrable=False, ) diff --git a/src/python/pants/backend/python/rules/pytest_runner_integration_test.py b/src/python/pants/backend/python/rules/pytest_runner_integration_test.py index b770d64eca1..7e75e8e9875 100644 --- a/src/python/pants/backend/python/rules/pytest_runner_integration_test.py +++ b/src/python/pants/backend/python/rules/pytest_runner_integration_test.py @@ -369,10 +369,7 @@ def test_conftest_injection(self) -> None: relpath=PurePath(self.source_root, self.conftest_source.path).as_posix(), contents=self.conftest_source.content.decode(), ) - - self.create_file( - relpath=PurePath(self.source_root, "BUILD").as_posix(), contents="python_tests()", - ) + self.add_to_build_file(self.source_root, "python_library()") result = self.run_pytest(passthrough_args="-s") assert result.status == Status.SUCCESS diff --git a/src/python/pants/backend/python/target_types.py b/src/python/pants/backend/python/target_types.py index 053a54b0e31..01cf2972701 100644 --- a/src/python/pants/backend/python/target_types.py +++ b/src/python/pants/backend/python/target_types.py @@ -240,7 +240,7 @@ class PythonBinary(Target): class PythonTestsSources(PythonSources): - default = ("test_*.py", "*_test.py", "tests.py", "conftest.py") + default = ("test_*.py", "*_test.py", "tests.py") class PythonTestsTimeout(IntField): @@ -281,6 +281,9 @@ class PythonTests(Target): These may be written in either Pytest-style or unittest style. + All test util code, including `conftest.py`, should go into a dedicated `python_library()` + target and then be included in the `dependencies` field. + See https://www.pantsbuild.org/docs/python-test-goal. """