如何从 TestSuite 中提取测试用例列表? [英] How can I extract a list of TestCases from a TestSuite?

查看:39
本文介绍了如何从 TestSuite 中提取测试用例列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 的单元测试和简单的代码,如下所示:

I am using Python's unittest with simple code like so:

suite = unittest.TestSuite()
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module1))
suite.addTest(unittest.defaultTestLoader.loadTestsFromTestCase(module2))

但是,我想在套件收集到每个测试后对它们进行一些自定义操作.我想我可以做这样的事情来迭代套件中的测试用例:

However, I am wanting to do some custom things to each test after they have been gathered by the suite. I thought I could do something like this to iterate over the test cases in suite:

print suite.countTestCases()
for test in suite:             # Also tried with suite.__iter__()
    # Do something with test
    print test.__class__

但是,对于我加载的尽可能多的测试用例,它只会打印

However, for as many test cases as I load, it only ever prints

3
<class 'unittest.suite.TestSuite'>

有没有办法从套件中获取 TestCase 类的所有对象?我应该通过其他方式加载测试用例来促进这一点吗?

Is there a way I can get all the objects of class TestCase from the suite? Is there some other way I should be loading test cases to facilitate this?

推荐答案

尝试

  for test in suite:
    print test._tests

这篇关于如何从 TestSuite 中提取测试用例列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆