我正在尝试为我一直在从事的项目执行测试用例。我以前可以成功执行单元测试,但现在出错了。我确信没有任何库的更新或路径的更改。我试图查看源代码并找出它出错的原因,但还没有成功。对此的任何帮助将不胜感激。
Python 版本 - 3.7.1
下面的示例代码
import unittest
class MyTestCase(unittest.TestCase):
def test_dummy(self):
self.assertEqual(2+2,4)
我在cmd中使用了以下命令来执行测试。
C:\Users\Yadada\Desktop\repo\mwe\mwe>python -m unittest tests\test_file.py
我的文件夹结构是
MWE -|
|_tests - |
|_test_file.py
预期的输出是成功执行的测试,因为它是一个简单的测试。但我最终收到以下错误
strclass
ERROR: test_file (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: test_file
Traceback (most recent call last):
File "C:\Users\yadada\AppData\Local\Continuum\anaconda3\lib\unittest\loader.py", line 156, in loadTestsFromName
module = __import__(module_name)
ModuleNotFoundError: No module named 'tests.test_file'
----------------------------------------------------------------------
Ran 1 test in 0.001s
原文由 Bee 发布,翻译遵循 CC BY-SA 4.0 许可协议
在测试文件夹中放置一个空的
__init__.py
文件后,问题得到解决。有关其工作原理的更好解释,请参阅 What is init.py for?
谢谢@aws_apprentice 的帮助。