1. loadTestsFromName()
如果想要运行某个测试用例类下面的某条测试用例,可以选TestLoader类里面的loadTestsFromName方法?
用法:loadTestsFromName(name,module = None)
传入的参数name必须是个string,且string需要有这种格式"module.class.method"
源码分析:
def loadTestsFromName(self, name, module=None):
"""Return a suite of all test cases given a string specifier.
The name may resolve either to a module, a test case class, a
test method within a test case class, or a callable object which
returns a TestCase or TestSuite instance.
The method optionally resolves the names relative to a given module.
"""
parts = name.split('.') # 将传入的参数name,以 . 的形式进行切割来,故pasts是个list
if module is None:
parts_copy = parts[:] #parts_copy 复制了 parts,eg: parts_copy =[module,class,method]

本文详细介绍了Python的unittest模块中TestLoader的loadTestsFromName()方法,该方法用于根据字符串指定加载测试用例。通过分析源码,我们可以看到此方法可以处理模块、测试用例类、测试方法等多种情况,并返回相应的TestSuite。理解这个方法对于自定义测试套件和执行特定测试用例至关重要。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



