cool-yacht-37128
07/27/2022, 9:53 AMsrc
|_ python
|_ package
tests
|_python
|_package
- test_1.py
- test_2.py
- BUILDWith a import in test_2.py like
from test_1 import function
However the test fails this import unless I explicitly provide python_tests the source for tests/python/package.
python_tests(name=“tests”, sources=[“tests/python/package”])Strangely while the above works, it gives a Unmatched glob warning ([WARN] Unmatched glob from tests/python/package:tests’s
sources field: “tests/python/package/tests/python/package”refined-addition-53644
07/27/2022, 10:04 AMpython_test_utils and then make that as dependencyrefined-addition-53644
07/27/2022, 10:08 AMcool-yacht-37128
07/27/2022, 1:28 PMpython_tests back to itself (tests/python/package:tests) did the trick. Imports now work, and no more warnings.
python_test_utils looks more like it would be more appropriate for test utility files such as stub files (*.pyi) and conftest.py than actual test files.bitter-ability-32190
07/27/2022, 1:58 PMsources field defines which files the generator target should use for generating targets (It's also relative to the build dir). So sources=["tests/python/package"] is saying there is one Python file named tests/python/package. Then it warns because there is no file at that path.
Usually you don't need to change it, as the default https://www.pantsbuild.org/docs/reference-python_tests#codesourcescode is usually correctbitter-ability-32190
07/27/2022, 2:00 PMbitter-ability-32190
07/27/2022, 2:02 PMoverrides field to augment the dependencies of the single test file.
See https://www.pantsbuild.org/docs/reference-python_tests#codeoverridescodehappy-kitchen-89482
07/27/2022, 2:20 PMfunction an actual test function or a helper function?cool-yacht-37128
07/27/2022, 10:05 PMsrc
|_ python
|_ package
|_file_3.py
- init.py
tests
|_python
|_package
- test_1.py
- test_2.py
- BUILDtest_1.py contains a wrapper around a function imported
from package.file_3
in test_2.py I’m trying to import that wrappercool-yacht-37128
07/27/2022, 10:06 PMpackage. Should the tests/python/package directory name be changed?happy-kitchen-89482
07/27/2022, 10:53 PMpython_test_utils target, rather than have it be owned by a python_tests, since you're not using it as a test itselfhappy-kitchen-89482
07/27/2022, 10:54 PMhappy-kitchen-89482
07/27/2022, 10:55 PMpackage something like package_tests to avoid this problem, since introducing a namespace package has its own complications, and isn't worth doing just for tests I thinkcool-yacht-37128
07/28/2022, 12:26 AM