我正在关注 python unittest 进行一些测试,并使用 discover 函数将测试打包到套件中。但是,当我尝试使用 unittest 运行测试时,出现此错误:
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'
这是 run_tests.py 的样子:
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)
原文由 frank.hsueh 发布,翻译遵循 CC BY-SA 4.0 许可协议
有一个非常相似的错误,但不涉及符号链接。
这是我的目录布局。
foo 和 bar 被认为是顶级包,其中 test 是所有测试用例。
在intellij中运行unittest时,我使用了以下参数:
test
文件夹此设置给了我
Start directory is not importable
错误。我在 unittest.loader.py 中追踪了源代码,发现它检查测试文件夹中的
__init__.py
。所以解决方案是在我的test
文件夹中添加一个__init__.py
。希望这对某人有帮助。- 编辑 -
我在 python 3.6,mac osx