首页 > 其他 > 详细

单元测试框架处理多组数据的另一种写法:基于构造函数和超继承

时间:2018-08-20 22:24:23      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:runner   测试类   重写   expect   testcase   result   urn   see   rda   

众所周知,在单元测试框架中引入ddt,可以将一组数据分解为多组数据,从而实现一条数据对应一个测试用例。但是除此之外,有没有别的办法来实现这个目的呢?

一. 代码部分

1. 创建一个被测函数math_method.py

class MethMethod():
    def add_test(self,a,b):
        return (a+b)

2. 创建测试用例test_math_method.py

import unittest
from homework.math_method import MethMethod


#测试类

class TestMathMethod(unittest.TestCase):

    def __init__(self, methodName, a, b, expected):
        super(TestMethMethod, self).__init__(methodName)
        self.a = a
        self.b = b
        self.expected = expected

    #测试用例

    def test_add(self):
        try:

            self.assertEqual(result, self.expected)
        except Exception:
            print("出错了")
        else:
            print("计算结果是:{}".format(result))

3. 创建测试数据test_data.txt

[{"a":2,"b":3,"expected":5},{"a":-1,"b":-2,"expected":-3},{"a":3,"b":-1,"expected":-2},{"a":-3,"b":-1,"expected":-4}]

4. 创建测试集test_suite.py

import unittest
from homework.test_math_method import TestMathMethod
import HTMLTestRunnerNew

fs= open("test_data.txt")
datas = fs.read()
datas = eval(datas)
print(type(datas))


if __name__ == __main__:

    suite=unittest.TestSuite()
    for item in datas:
        suite.addTest(TestMathMethod("test_add", item[a], item[b], item[expected]))
    file=open("test_report.html","wb+")
    runner=HTMLTestRunnerNew.HTMLTestRunner(file,title="加法单元测试报告",tester="july")
    runner.run(suite)

二. 代码分析

1. 测试用例类中需要定义一个构造函数,这个构造函数继承了父类的属性,也就是unittest.TestCase的methodName参数

同时,在子类中也重写了构造函数,添加了参数a, b, expected,而这三个则是测试用例函数test_add需要用到的

class TestMethMethod(unittest.TestCase):

    def __init__(self, methodName, a, b, expected):
        super(TestMethMethod, self).__init__(methodName)
        self.a = a
        self.b = b
        self.expected = expected

请看TestCase的源码

class TestCase(object):

failureException = AssertionError longMessage = True maxDiff = 80*8 # If a string is longer than _diffThreshold, use normal comparison instead # of difflib. See #11763. _diffThreshold = 2**16 # Attribute used by TestSuite for classSetUp _classSetupFailed = False def __init__(self, methodName=runTest): """Create an instance of the class that will use the named test method when executed. Raises a ValueError if the instance does not have a method with the specified name. """ self._testMethodName = methodName self._outcome = None self._testMethodDoc = No test try: testMethod = getattr(self, methodName) except AttributeError: if methodName != runTest: # we allow instantiation with no explicit method name # but not an *incorrect* or missing method name raise ValueError("no such test method in %s: %s" % (self.__class__, methodName)) else: self._testMethodDoc = testMethod.__doc__ self._cleanups = [] self._subtest = None

2. 此时回到测试集中,看下面一段,TestMathMethod类中本来只有一个参数,methodName,也就是测试用例名,但此时的TestMathMethod已经被我们改写了:不仅继承了父类的methodName方法,而且构造函数中新增了三个参数a, b, expected,这样我们在收集测试用例时,可以为TestMathMethod类添加四个参数:methodName, a, b, expected。通过for循环遍历一组数据,这样可以将多组数据作为多个测试用例添加到测试集中

for item in datas:
        suite.addTest(TestMathMethod("test_add", item[a], item[b], item[expected]))

三. 比较

ddt的方法:简单,因为涉及到装饰器,不易理解

构造函数:略显繁琐,但容易理解

 

单元测试框架处理多组数据的另一种写法:基于构造函数和超继承

标签:runner   测试类   重写   expect   testcase   result   urn   see   rda   

原文:https://www.cnblogs.com/cnhkzyy/p/9508358.html

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 bubuko.com 版权所有 鲁ICP备09046678号-4
打开技术之扣,分享程序人生!
             

鲁公网安备 37021202000002号