These are the directories of my module:
mymodule
|-__init__.py
|-file1.py
|-file2.py
|-test
|-__init__.py
|-test_file1.py
|-test_file2.py
test_file1.py
contains this command: from .. import file1
.
To run the tests I did this (on the command line): python3 -m unittest test.test_file1
. (When I ran that command for the whole test
directory it just told me "everything is fine" but didn't find my tests.)
The answer (also in the command line, of course) was (without a huge part of the stacktrace):
File "/media/me/my_usb/backup me/myfolder/django projects/django-mymodule/mymodule/test/test_file1.py", line 1, in <module>
from .. import file1
ValueError: attempted relative import beyond top-level package
What to do to fix this? What's best-practice with tests in multiple files?
Edit: I tried a few things as suggested, and that's what I have done:
I changed the tests direction (as suggested in the Hitchhikers guide to python. That's what it looks now:
modulewrapper |-mymodule (with the 2 files in it) |-... (docs, readME and this stuff) |-tests |-test_file1.py |-test_file2.py
I imported
mymodule
after inserting the direction like this (I added this code in the beginning of every test file):import sys sys.path.insert(0, '../mymodule') import file1
I started the test as usual: python3 -m unittest test_file1
from the tests directory.
What now happened is this: (first the relevant part of the stacktrace, then my guess):
File "/media/me/my usb/backup me/my folder/django projects/django-mymodule/tests/test_file1.py", line 4, in <module>
import file1
File "../mymodule/file1.py", line 4, in <module>
from .file2 import MyClass1, MyClass2
SystemError: Parent module '' not loaded, cannot perform relative import
How to deal with that new problem? (or is it the same as before? It seems hardly to be a kind of best_practice to change working code to be able to run tests.)
Edit2: For now, I jusr replaced from .file2 import someclass
by from file2 import someclass
. Can this have any negative side effects?