我的unittest文件夹以这种方式组织.
.
|-- import
| |-- import.kc
| |-- import.kh
| `-- import_test.py
|-- module
| |-- module.kc
| |-- module.kh
| `-- module_test.py
`-- test.py
Run Code Online (Sandbox Code Playgroud)
我想简单地运行test.py我的每个*_test.py使用unittest python模块.目前,我的test.py包含
#!/usr/bin/env python
import unittest
if __name__ == "__main__":
suite = unittest.TestLoader().discover('.', pattern = "*_test.py")
unittest.TextTestRunner(verbosity=2).run(suite)
Run Code Online (Sandbox Code Playgroud)
python文档说它应该自动在子文件夹中发现我的测试.但事实并非如此.
目前,它只输出
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Run Code Online (Sandbox Code Playgroud)
我确定这不是我的*_test.py文件的问题,因为当我将它们移动到根目录时,它工作正常..我做错了什么?
什么正是我需要做的,使Python的unittest工作?我检查了官方文档,SO问题,甚至尝试使用nose,但到目前为止没有任何工作.我做错了什么?
bash:~/path/to/project/src/tests$ ls -l
total 8
-rw-r--r-- 1 myuser myuser 342 Out 11 11:51 echo_test.py
-rw-r--r-- 1 myuser myuser 71 Out 11 11:28 __init__.py
bash:~/path/to/project/src/tests$ python -m unittest -v echo_test
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
bash:~/path/to/project/src/tests$ python -m unittest discover
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
bash:~/path/to/project/src/tests$ cat echo_test.py
import unittest
class EchoTest(unittest.TestCase):
def fooTest(self):
self.assertTrue(1==1)
def barTest(self):
self.assertTrue(1==2)
#suite = unittest.TestLoader().loadTestsFromTestCase(TestEcho)
#unittest.TextTestRunner(verbosity=2).run(suite)
if __name__ == '__main__':
unittest.main()
Run Code Online (Sandbox Code Playgroud)
正如你所看到的,测试只是没有运行,我不知道为什么(因为我不是python程序员).仅供参考,我使用的__init__.py是python …