the python unittest ddt unit testing framework implemented using a data driver -ddt, data, unpack use Introduction

ddt is a third-party modules, to be installed: pip install -i https://pypi.douban.com/simple ddt

Demo simple introduction, including the use of data and unpack:

import unittest
from ddt import ddt, data, unpack


@ddt
class TestAdd(unittest.TestCase):
    @data ( * [[. 1, 2,. 3], [. 4,. 5,. 9 ]])
     # Data decorating method, and for the same loop, each data variable list, and then passed to the method to be decorated a parameter 
    # there are few data, performs several times with Example 
    # first pass [2,3], the second pass [4,5,9] 
    DEF test_demo1 (Self, data):
        res = data[0] + data[1]
        expected = data[2]
        try:
            self.assertEqual(expected, res)
        except AssertionError as e:
            raise e

    @data(*[{'a': 0, 'b': 0, 'expected': 0}, {'a': 1, 'b': 1, 'expected': 2}])
    def test_demo2(self, data):
        res = data['a'] + data['b']
        expected = data['expected']
        try:
            self.assertEqual(expected, res)
        except AssertionError as e:
            raise e

    @data(*[[10, 20, 30], [40, 50, 90]])
    @unpack
    # Add to unpack split according to the comma, into a three parameters --- Test Method use three parameters to receiving 
    DEF test_demo3 (Self, A, B, expected):
        res = a + b
        try:
            self.assertEqual(expected, res)
        except AssertionError as e:
            raise e

    @data(*[{'a': 0, 'b': 0, 'expected': 0}, {'a': 1, 'b': 1, 'expected': 2}])
    @unpack
    # If the dictionary, then use his key as a parameter for data reception 
    DEF test_demo4 (Self, A, b, expected):
        res = a + b
        try:
            self.assertEqual(expected, res)
        except AssertionError as e:
            raise e

Guess you like

Origin www.cnblogs.com/benben-wu/p/12145661.html