I have the following directory structure:
.
├── README.md
├── src
│ ├── __init__.py
│ └── foo.py
└── test
├── __init__.py
└── runner.py
└── test_foo.py
Where the test files look like this:
test_foo.py
import unittest
from ..src.foo import *
class TestFoo(unittest.TestCase):
def setUp(self):
pass
def test_foo(self):
foo = Foo()
res = foo.get_something('bar')
if res is None:
self.fail('something bad happened')
if __name__ == '__main__':
unittest.main(self)
runner.py
import unittest
# import test modules
import test_foo
# initialize the test suite
loader = unittest.TestLoader()
suite = unittest.TestSuite()
# add tests to the test suite
suite.addTests(loader.loadTestsFromModule(test_foo))
# initialize a runner, pass it your suite and run it
runner = unittest.TextTestRunner(verbosity=3)
result = runner.run(suite)
I would like to run all of my tests from the parent directory in a testsuite so I attempt to call unites like this:
python -m test/runner.py
But it complains with the following:
$ python -m test/runner.py
/usr/bin/python: Import by filename is not supported.
if I move to the test directory, I get a different error:
$ python -m runner
File "test_foo.py", line 2, in <module>
from ..src.foo import *
ValueError: Attempted relative import in non-package
I would like to keep all test related stuff in the parent/test directory if possible.
Any idea what I'm doing wrong here?
Thanks!