python - 使用 PyUnit 运行所有测试用例

标签 python unit-testing python-unittest

我记得在 JUnit 中,有一种方法可以运行您创建的每个测试用例或仅运行选定文件中的测试用例。

有没有办法在 PyUnit 中完成这个任务?

我可以轻松运行当前打开的文件中的测试,但我希望选择运行所有测试(分布在不同的文件中)。

我见过 TestSuite,但我的理解是,每次创建新的 TestCase 时都需要手动将 TestCase 添加到 TestSuite。我希望能够自动完成此任务。

例如,我有以下测试用例(每个测试用例都有多个 test<Method Name> 方法):

class TestFamily(unittest.TestCase)
class TestFamilyMember(unittest.TestCase)

如何运行 TestFamily 中的所有测试和 TestFamilyMember

替代方案

查看nosetests

最佳答案

您是否尝试过在 test discovery mode 中运行单元测试? ?

在项目目录中,输入python -m unittest discovery。这将运行当前目录中可直接导入的文件中的所有测试。

来自 PyUnit/unittest 文档,为此工作:

In order to be compatible with test discovery, all of the test files must be modules or packages importable from the top-level directory of the project

关于python - 使用 PyUnit 运行所有测试用例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10939305/

相关文章:

Python/Google 应用引擎数据存储性能

python |如何创建复杂的字典

python - factoryboy pytest session 管理

python - 在单元测试中修补时,Celery 任务调用的函数没有调用?

Python unittest - 在 0.000 秒内运行 0 次测试

python - Pandas 选择根计数 3 次的行

python - 如何从 scrapy 响应中获取 cookie 并将 cookie 设置为下一个请求?

c# - 如何使用内部异常对代码进行单元测试?

android - Robolectric 和 Android Studio 1.1.0 和库测试

python - 如何在python中对数据库连接pymysql进行单元测试?