Good Summer! Challenges with python_test_utils. Ra...
# general
p
Good Summer! Challenges with python_test_utils. Racking my brain for many days on getting it working. Want to keep the tests DRY but taking long enough I may resort back to copy pasta. Need a single setup utility to support the tests. So, python_test_utils read like the perfect fit. From the BUILD:
Copy code
python_test_utils(
    name="test_utils",
    sources=[
        "**/setup.py", "**/__init__.py",
    ],
)

python_tests(
    name="tests",
    dependencies=[
        "src/python/api-client:lib",
        ":test_utils",
        ":test_resource",
    ],
    sources=[
        "**/*_test.py",
    ],
    interpreter_constraints=parametrize(py38=["==3.8.*"]),
)

resource(
    name="test_resource", source="resource/api_client_map_good.json"
)
The setup.py exists in the same directory as the relative_test.py script referencing the classes in setup.py. I've run with --keep-sandboxes=always to inspect the temporary sandbox. It appears that the setup.py was copied into the sandbox.
Copy code
j13@guardian:/tmp/pants-sandbox-ez8dUf/test/python/api-client/lib/client/response$ ls -l
total 16
drwxrwxr-x 2 j13 j13   60 Jun 29 15:14 __pycache__
-rw-r--r-- 1 j13 j13 2334 Jun 29 15:13 relative_test.py
-rw-r--r-- 1 j13 j13 8846 Jun 29 15:13 setup.py
The relative_test.py imports setup.py as:
Copy code
from setup import APIClientTestSetup
However on running pants test - the test doesn't run.
Copy code
j13@guardian:~/AppDevelopment/github/hank-ai/mondo$ rm -rf /tmp/pants-sandbox-*; pants --keep-sandboxes=always test test/python/hank-ai-api-client/lib/client/response/relative_test.py -- -s
15:13:15.90 [WARN] Pants cannot infer owners for the following imports in the target test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38:

  * setup.APIClientTestSetup (line: 30)

If you do not expect an import to be inferrable, add `# pants: no-infer-dep` to the import line. Otherwise, see <https://www.pantsbuild.org/v2.16/docs/troubleshooting#import-errors-and-missing-dependencies> for common problems.
15:13:15.94 [INFO] Preserving local process execution dir /tmp/pants-sandbox-5g3WqC for Run Pytest for test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38
15:13:17.00 [ERROR] Completed: Run Pytest - test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38 - failed (exit code 5).
============================= test session starts ==============================
collected 0 items

- generated xml file: /tmp/pants-sandbox-5g3WqC/test.python.hank-ai-api-client.lib.client.response.relative_test.py@@@tests@@interpreter_constraints=py38.xml -

============================ no tests ran in 0.69s =============================

āœ• test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38 failed in 1.04s.
Without the python_test_utils, the relative_test.py test is run but rightfully complains about the missing import. If I comment the import line the test is still not executed.
Copy code
from utils.setup import APIClientTestSetup  # pants: no-infer-dep
Copy code
j13@guardian:~/AppDevelopment/github/hank-ai/mondo$ rm -rf /tmp/pants-sand*; pants --keep-sandboxes=always test test/python/hank-ai-api-client/lib/client/response/relative_test.py -- -s
10:14:44.96 [INFO] Preserving local process execution dir /tmp/pants-sandbox-0BDNiM for Determine Python dependencies for test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38
10:14:45.20 [INFO] Preserving local process execution dir /tmp/pants-sandbox-sbIiWr for Run Pytest for test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38
10:14:46.27 [ERROR] Completed: Run Pytest - test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38 - failed (exit code 5).
============================= test session starts ==============================
collected 0 items

- generated xml file: /tmp/pants-sandbox-sbIiWr/test.python.hank-ai-api-client.lib.client.response.relative_test.py@@@tests@@interpreter_constraints=py38.xml -


============================ no tests ran in 0.69s =============================



āœ• test/python/hank-ai-api-client/lib/client/response/relative_test.py:../../../tests@interpreter_constraints=py38 failed in 1.05s.
Any and all insights appreciated. Thank you.
h
My guess is that the reason is that
setup
and
utils
are modules rather than packages. Try using absolute paths. (e.g
from my_package.utils.setup import APIClientTestSetup
).
p
It is a module I believe. setup.py just contains the imports it needs and a singular class at the moment.
Copy code
class APIClientTestSetup(unittest.TestCase):
Import pathing is so confounding sometimes - Here's the path in the sandbox.
Copy code
(eoan)j13@guardian:/tmp/pants-sandbox-XtcypX/test/python/hank-ai-api-client/lib/client/response$ ls
__pycache__  relative_test.py  setup.py
I'm presuming that the import path for APIClientTestSetup would/could be relative since it happens to be in the same directory. Regardless I perhaps have shot myself in the foot with the intermediate 'hank-ai-api-client' containing hyphens as if I try the absolute path relative to the sandbox root when the relative_test.py is executed it is invalid syntax.
Copy code
import test.python.hank-ai-api-client.lib.client.response.setup APIClientTestSetup
Copy code
E       from test.python.hank-ai-api-client.lib.client.response.setup import APIClientTestSetup  # pants: no-infer-dep
E                            ^
E   SyntaxError: invalid syntax
I'll rename the intermediate directory name to be underscored to see it it makes a difference. After renaming with underscores, moving ~/.cache/pants, updating .gitignore and re-running pants is reporting in error:
Copy code
!test/python/hank_ai_api_client/lib/
Copy code
j13@guardian:~/AppDevelopment/github/hank-ai/mondo$ rm -rf /tmp/pants-sand*; pants --keep-sandboxes=always test test/python/hank_ai_api_client/lib/client/response/relative_test.py -- -s
10:52:01.40 [ERROR] 1 Exception encountered:

Engine traceback:
  in `test` goal

IntrinsicError: Unmatched glob from CLI arguments: "test/python/hank_ai_api_client/lib/client/response/relative_test.py"

Do the file(s) exist? If so, check if the file(s) are in your `.gitignore` or the global `pants_ignore` option, which may result in Pants not being able to see the file(s) even though they exist on disk. Refer to <https://www.pantsbuild.org/v2.16/docs/troubleshooting#pants-cannot-find-a-file-in-your-project>.
h
Maybe adding a
__init__.py
will resolve it? (disclaimer: I'm not a maintainer).
p
Peppered
__init__.py
all over. It at least get's past the intrinsicError but still gets an exit(5) on interpreter_contraints.
h
What's the error with the interpreter-contraint?
p
So, I surmise that the "Completed: Run Pytest ... failed (exit code 5)" is preventing the inclusion of the requested tests to execute as the contents of the interpretre_contraints file is essentially empty.
Copy code
<?xml version="1.0" encoding="utf-8"?><testsuites><testsuite name="pytest" errors="0" failures="0" skipped="0" tests="0" time="0.764" timestamp="2023-07-06T11:30:08.366539" hostname="guardian" /></testsuites>
I'll see if any pytest exit codes might match - but unclear if it's from pants or pytest or just where. (I'll see to about increasing verbosity during pants run.)
Pytest: Exit code 5. No tests were collected. Which is inline with the output. Running pants test ... -- -s -vvv provided zero additional details than without.
b
Does the test run with pytest outside of pants? (i.e. against the files in the main repo)
p
To my great shagrin - yes; 'pytest relative_test.py' failed. I wasn't using the pytest proper function naming. I've corrected, and re-tested with pants and the referenced util file is correctly imported and the test executes. Happy Summer!
šŸ‘ 1