0

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!

Pat Mustard
  • 1,852
  • 9
  • 31
  • 58

1 Answers1

0

try python -m unittest test.runner. Here's the structure I used.

├── README.md
├── src
│   ├── __init__.py
│   └── foo.py
└── test
    ├── __init__.py
    └── test_foo.py
    └── runner.py

my foo.py contains a simple function, foo. I changed the import statement in the test_foo.py to the following:

from src.foo import foo.

I also ran directly from runner.py with the following changes.

from test import test_foo

and then in the command line at the parent directory, python -m unittest test.runner

This structure only works from the parent directory. If I try it in the test directory, it fails to import.

e.s.
  • 1,351
  • 8
  • 12
  • I don't want to use the discover method because I want to explicitly add tests to my suite. I tried calling `python -m unittest test/runner.py` but got some errors again: `ImportError: Import by filename is not supported.` – Pat Mustard May 04 '18 at 01:23
  • what python version? if 2, try `from __future__ import absolute_import`. – e.s. May 04 '18 at 04:12
  • I tried with 2 and 3. Surely I don't need to import additional libs just to run unites with a standard directory structure? – Pat Mustard May 04 '18 at 04:14
  • I get this with python3: `ModuleNotFoundError: No module named 'test.runner'` – Pat Mustard May 04 '18 at 04:19
  • all i can say is that, "well it works on my computer" :( . in py2.7 and 3.5. and now i really want to know why it doesn't on yours. – e.s. May 04 '18 at 05:00