本站资源收集于互联网,不提供软件存储服务,每天免费更新优质的软件以及学习资源!

Pythonunittest无法发现测试文件?如何正确使用unittestdiscover?

网络教程 app 1℃

Pythonunittest无法发现测试文件如何正确使用unittestdiscover

python 的 unittest 自动发现测试文件

python 的 unittest 框架可以自动查找并运行测试文件。但是,有时候我们可能会遇到某些测试文件无法被发现的情况。

问题描述

在一个项目目录下,有两个测试文件 test_meta.py 和 test_sample.py,它们位于一个子目录 testing 中。使用命令 python -m unittest testing 运行测试,却无法发现这两个文件。

解决方案

根据 unittest-discovery 文档,直接使用 python -m unittest 等同于 python -m unittest discover。因此,如果要指定目录进行测试发现,需要显式使用 discover 子命令。

正确命令

正确的命令应该是:

python -m unittest discover testing

这样,unittest 框架将能够在 testing 目录中自动查找所有以 test 开头的 python 文件,并运行其中的测试。

以上就是Python unittest 无法发现测试文件?如何正确使用unittest discover?的详细内容,更多请关注范的资源库其它相关文章!

转载请注明:范的资源库 » Pythonunittest无法发现测试文件?如何正确使用unittestdiscover?

喜欢 (0)