Hey all, I am having a problem testing one of my p...
# general
c
Hey all, I am having a problem testing one of my python modules because it can't seem to find one of my internal dependencies that is in the test folder. Here is my project structure:
Copy code
└── src
    ├── python
    │   ├── platform_hardware
    │   │   ├── folder1
    │   │   ├── folder2
    │   │   ├── tests
    │   │   |   ├── BUILD
    │   │   |   ├── test_file1.py
    │   │   |   ├── test_file2.py
    │   │   |   └── mock_interface.py
    │   │   ├── __main__.py
    │   │   └── BUILD
Here is the BUILD file in my tests directory:
Copy code
python_test_utils(
    name="test_utils",
    sources=["mock_interface.py"]
)

python_tests(
    name="tests",
    dependencies=[":test_utils"]
)
Both of my test files import from the mock_interfact.py file, and that import looks like this:
Copy code
from tests.mock_interface import MockInterface
When I try to run my tests using
pants test src/python/platform_hardware/tests:
I get the following error:
Copy code
from tests.mock_interface import MockInterface
E   ModuleNotFoundError: No module named 'tests'
I feel like there is a really simple solution here that I'm missing, but I've been pulling my hair out for hours trying to get these tests to run. I've tried using fully qualified names in the BUILD file, I've tried creating the target for this file as both a file or python source, and I've tried importing this file in all the ways that make sense to me. What am I missing/doing wrong here?
b
tests
isn’t a package so I don’t think you can import from it like that. Try adding a
___init___.py
file inside the
tests
directory and see if that works?
c
Sorry, I didn't include that for brevity, but there is an init.py in the tests folder
b
Is there one in
platform_hardware
?
c
Yes
b
Got it. You should be able to inspect the contents of the test sandbox by running:
Copy code
pants --keep_sandboxes=on_failure test src/python/platform_hardware/tests::
Since it fails, the sandbox should be retained on local disk, and you can go to that folder and see what files are inside. I’m guessing something is not being added to the sandbox where it should be.
c
I'm not sure exactly what I'm looking for here, but yeah the mock_interface file isn't in here
b
Are the test files there / other source files?
c
The test file is there. As for the source files, there is only one source file. The source file that I'm seeing is imported by the test, but the source file also has imports of its own, and none of those are present.
b
I think it might be that the import path is incorrect, and you need to change:
Copy code
from tests.mock_interface import MockInterface
to:
Copy code
from platform_hardware.tests.mock_interface import MockInterface
c
That gives me the same error, ModuleNotFoundError: No module named 'platform_hardware.tests'
Yeah I'm really confused as to what the issue is here. I've had similar issues in the past and I've never been able to solve it so I was really hoping to figure it out this time around now that my understanding of Pants is a bit better. But for now I'll just move it into a conftest file since I've always had success with those files working without issue.
e
how are your source roots set up? Not sure exactly what the problem is, but my suspicion is that you need to set up your source roots so that the imports can be read correctly
c
pants source roots returns 'src/python'
One thing to note is that this has only ever happened for files within test folders that do not have test in their name. So conftest is always found correctly, as are all our test files (either test_something.py or something_test.py)