我记得在 JUnit 中,有一种方法可以运行您创建的每个测试用例或仅运行选定文件中的测试用例。
有没有办法在 PyUnit 中完成这个任务?
我可以轻松运行当前打开的文件中的测试,但我希望选择运行所有测试(分布在不同的文件中)。
我见过 TestSuite,但我的理解是,每次创建新的 TestCase 时都需要手动将 TestCase 添加到 TestSuite。我希望能够自动完成此任务。
例如,我有以下测试用例(每个测试用例都有多个 test<Method Name>
方法):
class TestFamily(unittest.TestCase)
class TestFamilyMember(unittest.TestCase)
如何运行 TestFamily
中的所有测试和 TestFamilyMember
?
替代方案
最佳答案
您是否尝试过在 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/