python - `python -m unittest discover` 未发现测试

标签 python unit-testing nose unittest2

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/

相关文章:

Python吉他指板音高/频率实现

python - 诗歌 + Cython + 测试(Nosetests)

python - Nose / Nose : Import problems

python - 我可以在单元测试的设置阶段编译 numba 吗?

unit-testing - 如何在 CI 管道中对使用不属于 git 远程的模型文件的函数进行单元测试?

ios - tableView.cellForRow(at :) and tableView. dataSource?tableView(tableView:cellForRowAt:) 之间的区别

python - Motor 1.1 中如何分组?

Python - 使用 HTTPServer 处理并发 POST 请求并保存到文件

python - 如何将包含 '_' 的 JSON 字段拆分为子对象?

unit-testing - 在Grails中模拟mixin类