Python 的 unittest discover 没有找到我的测试!
我一直在使用 Nose 来发现我的单元测试,它工作正常。从我的项目的顶层,如果我运行 nosetests
我得到:
Ran 31 tests in 0.390s
现在 Python 2.7 单元测试 has discovery ,我试过用
python -m unittest discover
但我明白了
Ran 0 tests in 0.000s
我的目录结构是:
myproj/
reporter/
__init__.py
report.py
[other app modules]
tests/
__init__.py
test-report.py
[other test modules]
你知道为什么 unittest 的发现算法找不到测试吗?
我在 Windows 7 上使用 Python 2.7.1 和 Nose 1.0.0。
最佳答案
这种行为是故意的,但文档可以更清楚地说明这一点。如果您查看测试发现部分的第一段,它会说:
For a project’s tests to be compatible with test discovery they must all be importable from the top level directory of the project (in other words, they must all be in Python packages).
由此得出的结论是文件名也必须是有效的 Python 模块名。 test-report.py
未能通过该测试,因为 test-report
不是合法的 Python 标识符。
一个文档错误建议在 -p
模式选项的文档中明确提及这一点,这可能是一个很好的前进方向。
关于python - `python -m unittest discover` 未发现测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5088960/