I'm following python unittest to make some test and use discover function to pack tests to a suite. However when I try to run the test with unittest, I get this error:
Traceback (most recent call last):
File "D:/Project/run_tests.py", line 12, in <module>
suite2 = unittest.defaultTestLoader.discover(dir2, pattern='test*.py')
File "C:\Python\Python36-32\lib\unittest\loader.py", line 338, in discover
raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'D:\\Project\\dir2'
This is how the run_tests.py looks like:
import unittest
if __name__ == "__main__":
dir1 = "./test1"
suite1 = unittest.defaultTestLoader.discover(dir1, pattern='test*.py')
runner1 = unittest.TextTestRunner()
runner1.run(suite1)
dir2 = "./tes2"
suite2 = unittest.defaultTestLoader.discover(dir2, pattern='test*.py')
runner2 = unittest.TextTestRunner()
runner2.run(suite2)
Had a very similar error, but no symlink involves.
This is my directory layout.
- project root
- foo
- bar
- test
foo and bar are considered top level packages, where test is all the test cases live.
When running unittest in intellij, I used the following parameters:
test
folderThis setup gave me the Start directory is not importable
error.
I traced the source in unittest.loader.py and realize it checks the test folder for __init__.py
. So the solution is to add a __init__.py
in my test
folder. Hope this helps someone.
-- edit --
I'm on python 3.6, mac osx
I had a very similar problem testing on CI Pipeline. In my case there was a level of indirection folder/topleveldirectory/tests
so I added -s to my unittest command:
python -m unittest discover -s folder/topleveldirectory/tests
There's a similar question with a helpful answer here.
However, this can happen if you're using an IDE like PyCharm in Linux and opening files in a soft-linked directory. It seems the editor which is running the tests gets confused about the two paths to the same module and says one doesn't exist. Putting it all in one location without any softlinks fixed this for me.
That issue always occurs to me regularly in my python projects using PyCharm/IntelliJ. So I needed to combine both hints above in order to get it running on my platform (usually Linux). So I got it up and running with the following steps:
__init__.py
in my unit testing directory.When referring to a symbolically linked directory the following error occurred when trying to run the unit tests:
Traceback (most recent call last): File "/home/z003yb2k/.IntelliJIdea2019.2/config/plugins/python/helpers/pycharm/_jb_unittest_runner.py", line 35, in main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner, buffer=not JB_DISABLE_BUFFERING) File "/usr/lib/python3.7/unittest/main.py", line 100, in __init__ self.parseArgs(argv) File "/usr/lib/python3.7/unittest/main.py", line 124, in parseArgs self._do_discovery(argv[2:]) File "/usr/lib/python3.7/unittest/main.py", line 244, in _do_discovery self.createTests(from_discovery=True, Loader=Loader) File "/usr/lib/python3.7/unittest/main.py", line 154, in createTests self.test = loader.discover(self.start, self.pattern, self.top) File "/usr/lib/python3.7/unittest/loader.py", line 349, in discover tests = list(self._find_tests(start_dir, pattern)) File "/usr/lib/python3.7/unittest/loader.py", line 387, in _find_tests name = self._get_name_from_path(start_dir) File "/usr/lib/python3.7/unittest/loader.py", line 371, in _get_name_from_path assert not _relpath.startswith('..'), "Path must be within the project" AssertionError: Path must be within the project
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With