2
我跟隨python unittest進行一些測試並使用discover函數將測試打包到一個套件。 然而,當我嘗試運行單元測試與測試,我得到這個錯誤:python - unittest - ImportError:啓動目錄不可導入
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)