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

引用自:http://blog.csdn.net/kaku21/article/details/42124593

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

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

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

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

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

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

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

关于TestNG数据驱动的详情可以参考 http://testng.org/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,结果如下:

3db7e998214cb0ca5f785157f0a8734572d.jpg

元类的概念,请参考:

http://www.ibm.com/developerworks/cn/linux/l-pymeta/

http://blog.csdn.net/b2b160/article/details/4161189

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

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

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

http://www.cnblogs.com/fnng/p/8185172.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值