I have a relatively complicated project that is managed by Gradle. Most modules are Java-based, so I'm using IntelliJ (and not PyCharm) as the IDE. The Python plugin works fine for the one Python module, except that I can't get unit tests to work.
The directory structure is roughly like this:
project_root
|- aJavaModule
| |- ...
|- anotherJavaModule
| |- ...
|- python_module/
| |- src/
| |- __init__.py
| |- util.py
| |- test/
| |- __init__.py
| |- util.py
| ...
The test/util.py file is this:
import unittest
from util import util_func
class TestUtilities(unittest.TestCase):
def test_util_func(self):
...
There is a specific virtual environment set up for the Python module to run in. This is configured in the Project Structure -> Facets -> python_module as the Python Interpreter. I'm confident this is working as expected, because when working in the IDE it is properly finding relative files, dependent libraries, etc.
In fact, if I'm in test/util.py the IDE recognizes the from util import util_func: I can click through to the correct, etc.
If I right click on the test class and Create Run/Debug Configuration I can set all of that up. In this modal it includes Add content roots to PYTHONPATH and Add source roots to PYTHONPATH, so presumably the src and test directories are being found. (Note that python_module/src is marked as a source root in the Project Structure modal.) I've tried selecting that specific script as the test, everything in the folder, etc. I've also tried moving the test directory outside the src directory and marking it as a test directory. Again, the IDE can find the classes when editing files, but when I run the test it fails to.
Specifically, when I run the tests I get this:
Testing started at 5:39 PM ...
Traceback (most recent call last):
File "/Users/nathanielford/Library/Application Support/IntelliJIdea2016.3/python/helpers/pycharm/utrunner.py", line 172, in <module>
module = loadSource(a[0])
File "/Users/nathanielford/Library/Application Support/IntelliJIdea2016.3/python/helpers/pycharm/utrunner.py", line 65, in loadSource
module = imp.load_source(moduleName, fileName)
File "/Users/nathanielford/virtualenvironments/ideenv/lib/python3.5/imp.py", line 172, in load_source
module = _load(spec)
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/Users/nathanielford/repos/project_root/python_module/src/tests/util.py", line 2, in <module>
from util import util_func
ImportError: cannot import name 'util_func'
Process finished with exit code 1
What do I need to do to get the IDE test runner to recognize the correct environment and path variables? (Simply switching to PyCharm is not an option.)
Outputting the system path (sys.path) I get the following:
System path=['/Users/nathanielford/repos/project_root/python_module/src/tests', '/Users/nathanielford/repos/project_root/python_module/src', '/Users/nathanielford/repos/project_root/python_module', '/Users/nathanielford/Library/Application Support/IntelliJIdea2016.3/python/helpers/pycharm', '/Users/nathanielford/virtualenvironments/ideenv/lib/python35.zip', '/Users/nathanielford/virtualenvironments/ideenv/lib/python3.5', '/Users/nathanielford/virtualenvironments/ideenv/lib/python3.5/plat-darwin', '/Users/nathanielford/virtualenvironments/ideenv/lib/python3.5/lib-dynload', '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/opt/local/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/Users/nathanielford/virtualenvironments/ideenv/lib/python3.5/site-packages']
This seems to suggest that the src directory is, in fact, being found? So everything in that directory should be available?
The problem is due to namespaces in Python. Since both your test and the file being tested are named 'util.py', Python imports from the first one it comes across in the Python path.
Since the tests dir is before the parent directory in that path variable, it will try to import from the test itself, which doesn't have that function defined.
Simply renaming the test to "test_util.py" would resolve the namespace issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With