Reputation: 627
I wrote my own module, let's call it mymodule
. To improve whether it's working I wrote tests
. So that's what the structure became:
django-mymodule
|-mymodule (.py files in here)
|-tests (.py files in here, they are unittests)
|-... (docs and this stuff)
When module
is installed to my computer is has to use relative imports like this: from . import otherfile
. When it is started as test, the imports have to look like this: import otherfile
.
So I will have to do something like this:
if is_run_as_test:
import otherfile
else:
from . import otherfile
How does this is_run_as_test
looks? Or am I doing something really wrong?
Upvotes: 0
Views: 35
Reputation: 627
I was completely wrong. What I have to do is importing the whole package to the pythonpath and then using absolute imports. (In the tests file, of course.)
That means:
So I use from . import somefile
in mymodule
. To the tests I added this code:
import unittest
import sys
sys.path.insert(0, '../../django-mymodule')
from mymodule import file1
Thank you for the tipps!
Upvotes: 0
Reputation: 9601
You can achieve this by checking whether or not your testing framework has been imported.
It would be strange to have it imported in regular code, so one could assume that the code is being run during a test.
if 'unittest' in locals():
print('I am being run within a test')
Upvotes: 1