Hi there :wave: I am trying to incorporate Pants i...
# general
b
Hi there πŸ‘‹ I am trying to incorporate Pants in our CI to run tests. So far, we used pytest on the agents. A snippet of our repo is as below. I can successfully run the unit tests using
pants test
. However, when trying
pants test governance/app1/test/dataplatform/governance/app1/integration/
, I get
ModuleNotFoundError: No module named 'dataplatform.governance.app1.integration.fixtures
. I have seen many questions regarding
conftest.py
but could not really understand how to solve this issue? Any help is very appreciated πŸ™‚
Copy code
β”œβ”€β”€ app1
β”‚   β”œβ”€β”€ BUILD
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ main
β”‚   β”‚   β”œβ”€β”€ BUILD
β”‚   β”‚   β”œβ”€β”€ app.py
β”‚   β”‚   └── dataplatform
β”‚   β”‚       └── governance
β”‚   β”‚           └── app1
β”‚   β”‚               β”œβ”€β”€ BUILD
β”‚   β”‚               β”œβ”€β”€ client.py
β”‚   β”œβ”€β”€ test
β”‚   β”‚   β”œβ”€β”€ BUILD
β”‚   β”‚   β”œβ”€β”€ conftest.py
β”‚   β”‚   β”œβ”€β”€ dataplatform
β”‚   β”‚   β”‚   └── governance
β”‚   β”‚   β”‚       └── app1
β”‚   β”‚   β”‚           β”œβ”€β”€ integration
β”‚   β”‚   β”‚           β”‚   β”œβ”€β”€ BUILD
β”‚   β”‚   β”‚           β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚           β”‚   β”œβ”€β”€ fixtures
β”‚   β”‚   β”‚           β”‚   β”‚   β”œβ”€β”€ BUILD
β”‚   β”‚   β”‚           β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚           β”‚   β”‚   └── client_fixtures.py
β”‚   β”‚   β”‚           β”‚   β”œβ”€β”€ test_integration_client.py
β”‚   β”‚   β”‚           └── unit
β”‚   β”‚   β”‚               β”œβ”€β”€ BUILD
β”‚   β”‚   β”‚               β”œβ”€β”€ __init__.py
β”‚   β”‚   β”‚               β”œβ”€β”€ test_client.py
app1/test/BUILD
->
Copy code
python_test_utils(
    name="test_utils",
)
app1/test/conftest.py
->
Copy code
pytest_plugins = [
    "dataplatform.governance.app1.integration.fixtures.client_fixtures",
]
pants.toml
->
Copy code
[source]
root_patterns = [
    'main',
    'test'
]
βœ… 1
l
My guess would be that
main/dataplatform
shadows the
test/dataplatform
PYTHONPATH. Try changing
test/dataplatform
to
test/dataplatform1
and see if that helps. Generally, you will need to play a bit with a directory layout of your tests as suggested here https://docs.pytest.org/en/7.1.x/explanation/goodpractices.html#choosing-a-test-layout-import-rules
b
Thanks @little-pilot-11155. That might be it. For now, after renaming to
test/dataplatform1
, it is still failing with
ImportError: Error importing plugin "dataplatform1.governance.app1.integration.fixtures.client_fixtures": No module named 'dataplatform1.governance.app1.integration'
I will have a look at the source you provided.
b
It’s possible also that you’d need to add an explicit dependency in the BUILD file from your test code to the fixtures folder. Ex:
Copy code
python_test_utils(
    name="test_utils",
    dependencies=["app1/test/dataplatform/governance/app1/integration:target"]
)
b
After renaming to
test/dataplatform1
, I actually succeeded in running the integration tests by adding the dependency explicitly in
governance/app1/test/dataplatform1/governance/app1/integration/BUILD
Copy code
python_tests(
    name="tests",
    dependencies=[
        "//3rdparty/python:reqs",
        "//3rdparty/python:reqs0",
"governance/app1/test/dataplatform1/governance/app1/integration/fixtures"
    ],
)
I will test out your suggestion @better-van-82973 πŸ™ I guess this would allow me to keep the naming as
test/dataplatform
?
b
Yeah, if you specify the dependency name explicitly then you should be able to call it whatever you’d like to
b
Thanks works like a charm. In case it can help someone in the future, second solution, without needing to rename the
test/dataplatform
directory, is via:
governance/app1/test/BUILD
Copy code
python_test_utils(
    name="test_utils",
    dependencies=["governance/app1/test/dataplatform/governance/app1/integration:tests"]
)
Thanks again @little-pilot-11155 and @better-van-82973 ✌️
l
TIL πŸ™‚