1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python测试框架untest怎么循环执行_unittest如何在循环遍历一条用例时生成多个测试结果...

python测试框架untest怎么循环执行_unittest如何在循环遍历一条用例时生成多个测试结果...

时间:2022-12-27 14:32:07

相关推荐

python测试框架untest怎么循环执行_unittest如何在循环遍历一条用例时生成多个测试结果...

引用自:/kaku21/article/details/42124593

参考网址:/test-data-provider-using-python-metaclass.html

使用TestNG进行测试的时候,允许使用外部数据源来驱动测试方法的执行,举个例子:

我们有一个测试方法,而这个测试方法对应有10条测试数据,如果我们在测试方法中使用循环遍历这十条数据的话,很可能出现的问题是:

1.测试中的断言 assert 遇到一条数据执行结果错误时,退出测试方法

2.剩余的数据无法继续执行,产生的数据报告不完整

解决上述问题的方法,利用TestNG的dataprodvider在测试类中获取外部数据,然后传给测试方法,这样做到每一条数据驱动一次测试方法的执行,测试方法按数据量自动产生方法序号,报告测试结果。当某一条数据执行结果错误的时候,该方法对应的产生一次失败信息,但不影响下一条数据的执行。

在测试报告中,看到的是某一个方法,然后产生了多少条报告。

关于TestNG数据驱动的详情可以参考 /doc/documentation-main.html#parameters-dataproviders

我们要说的是,在python的unittest中没有类似的方法,当我们需要遍历测试数据的时候怎样更好的去查看结果生成报告呢?

目前我没有找到比较好的第三方组件,唯一的方式是自己来改写,参看下面的代码

import unittest

class DataProviderSupport(type):

def __new__(meta, classname, bases, classDict):

# method for creating our test methods

def create_test_method(testFunc, args):

return lambda self: testFunc(self, *args)

# look for data provider functions

for attrName, attr in classDict.items():

if attrName.startswith("dataprovider_"):

# find out the corresponding test method

testName = attrName[13:]

testFunc = classDict[testName]

# the test method is no longer needed

del classDict[testName]

# generate test method variants based on

# data from the data porovider function

i = 1

for args in attr():

classDict[testName + str(i)] = create_test_method(testFunc, args)

i += 1

# create the type

return type.__new__(meta, classname, bases, classDict)

这里其实用到了元类的概念,也就是,我们重写的type元类的__new__函数

验证一下:

class TestStringLength(unittest.TestCase):

__metaclass__ = DataProviderSupport

def dataprovider_test_len_function(): # no self!

yield ("abc", 3)

yield ("", 0)

yield ("a", 1)

def test_len_function(self, astring, expectedLength):

self.assertEqual(expectedLength, len(astring))

运行 python的unittest,结果如下:

元类的概念,请参考:

/developerworks/cn/linux/l-pymeta/

/b2b160/article/details/4161189

这个函数能够返回当前类的类名称,类字典,同时可以创建测试方法 即 create_test_method

当有多个测试数据对应一个测试方法时,我们把测试方法增加了序号,如果在后续引用测试方法时,需要在测试方法名称后面添加序号

例如 suite.addTest(Testdriver(methodname+sn))

/fnng/p/8185172.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。