You can't import from the parent directory without some voodoo. Here's yet another way that works with at least Python 3.6.
First, have a file test/context.py with the following content:
import sys import os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) Then have the following import in the file test/test_antigravity.py:
import unittest try: import context except ModuleNotFoundError: import test.context import antigravity Note that the reason for this try-except clause is that
- import test.context fails when run with "python test_antigravity.py" and
- import context fails when run with "python -m unittest" run from the new_project directory.
With this trickery they both work.
Now you can run all the test files within test directory with:
$ pwd /projects/new_project $ python -m unittest or run an individual test file with:
$ cd /projects/new_projecttest $ python test_antigravity Ok, it's not much prettier than having the content of context.py within test_antigravity.py, but maybe a little. Suggestions are welcome.