带有子文件夹的 Python unittest 发现 [英] Python unittest discovery with subfolders
问题描述
我的 unittest 文件夹是这样组织的.
My unittest folder is organized this way.
.
|-- import
| |-- import.kc
| |-- import.kh
| `-- import_test.py
|-- module
| |-- module.kc
| |-- module.kh
| `-- module_test.py
`-- test.py
我想简单地运行 test.py
以使用 unittest Python 模块运行我的每个 *_test.py
.
I'd want to simply run test.py
to run each of my *_test.py
using the unittest Python module.
目前,我的 test.py 包含
Currently, my test.py contains
#!/usr/bin/env python
import unittest
if __name__ == "__main__":
suite = unittest.TestLoader().discover('.', pattern = "*_test.py")
unittest.TextTestRunner(verbosity=2).run(suite)
python 文档说它应该自动在子文件夹中发现我的测试.但事实并非如此.
The python documentation says that it should automatically discover my test in the subfolders. But it does not.
目前只输出
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
我确定我的 *_test.py
文件没有问题,因为当我将它们移动到根目录时,它工作正常..我做错了什么?
I'm sure it is not a problem with my *_test.py
file, because when I move them into the root directory, it works fine..
What am I doing wrong?
推荐答案
在 import
和 module
目录中添加 __init__.py
.
Add __init__.py
in the import
and module
directories.
这篇关于带有子文件夹的 Python unittest 发现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!