python+unittest+ddt实现数据驱动测试

目录

  1. 前言
  2. ddt介绍
  3. 具体用法

前言

数据驱动测试:

  1. 避免编写重复代码
  2. 数据与测试脚本分离
  3. 通过使用数据驱动测试,来验证多组数据测试场景
    通常来说,多用于单元测试和接口测试

ddt介绍

Data-Driven Tests(DDT)即数据驱动测试,可以实现不同数据运行同一个测试用例。ddt本质其实就是装饰器,一组数据一个场景。
安装:
pip install ddt

具体用法

  1. data装饰器
from ddt import ddt, data
import unittest
import HTMLTestRunner
testcase_dir = r"C:\Users\shitou\git\commonProject\common\API"
def set_test_suite():
    """
    set case suite
    :return:
    """
    #定义测试套件
    test_suite = unittest.TestSuite()
    discover = unittest.defaultTestLoader.discover(testcase_dir, pattern = 'TS*.py', top_level_dir=None)
    if discover:
        for suite in discover:
            for test_case in suite:
                test_suite.addTest(test_case)
    return test_suite

@ddt
class MyTest(unittest.TestCase):
    @data(0,1,None,1==1,1>2,"test",[],{},(),False,True)
    def test(self,value):
        self.assertTrue(value)
        
if __name__=="__main__":
    test_suite = set_test_suite()
    fp = open("report.html", 'wb')
    runner = HTMLTestRunner.HTMLTestRunner(stream=fp, 
                               verbosity=2,
                               title='Test Report', 
                               description='Test Description')
    runner.run(test_suite)
    fp.close()

 命名:测试用例方法+序号
F  test_01_0 (TS001.MyTest)
ok test_02_1 (TS001.MyTest)
F  test_03_None (TS001.MyTest)
ok test_04_True (TS001.MyTest)
F  test_05_False (TS001.MyTest)
ok test_06_test (TS001.MyTest)
F  test_07___ (TS001.MyTest)
F  test_08 (TS001.MyTest)
F  test_09___ (TS001.MyTest)
F  test_10_False (TS001.MyTest)
ok test_11_True (TS001.MyTest)
<_io.TextIOWrapper name='' mode='w' encoding='utf-8'> 
Time Elapsed: 0:00:00.002117

python+unittest+ddt实现数据驱动测试_第1张图片

  1. unpack 自动处理元组,列表到多个参数;字典到多个关键字参数。
    2.1 元组
def add(a,b):
    return a+b

@ddt
class MyTest(unittest.TestCase):
    @data((1,2,3),(1,0,1),(0,0,0),(1,1,3))
    @unpack
    def test(self,a,b,c):
        self.assertEqual(add(a, b),c)
 命名:测试用例方法+序号+参数
# ok test_1__1__2__3_ (TS001.MyTest)
# ok test_2__1__0__1_ (TS001.MyTest)
# ok test_3__0__0__0_ (TS001.MyTest)
# F  test_4__1__1__3_ (TS001.MyTest)
# <_io.TextIOWrapper name='' mode='w' encoding='utf-8'> 
# Time Elapsed: 0:00:00.000931

2.2 字典

def add(a,b):
    return a+b

@ddt
class MyTest(unittest.TestCase):
    @data({"a":1,"b":1,"c":2},
          {"a":0,"b":0,"c":0},
          {"a":-1,"b":1,"c":0})
    @unpack
    def test(self,a,b,c):
        self.assertEqual(add(a, b),c)

# ok test_1 (TS001.MyTest)
# ok test_2 (TS001.MyTest)
# ok test_3 (TS001.MyTest) 

2.3 列表

@ddt
class MyTest(unittest.TestCase):
    @data([1,2,3],[1,0,1],[0,0,0],[1,1,3])
    @unpack
    def test(self,a,b,c):
        self.assertEqual(add(a, b),c)
 命名:测试用例方法+序号+参数
# ok test_1__1__2__3_ (TS001.MyTest)
# ok test_2__1__0__1_ (TS001.MyTest)
# ok test_3__0__0__0_ (TS001.MyTest)
# F  test_4__1__1__3_ (TS001.MyTest)
  1. file_data装饰器
    ddt支持从文件中加载数据,@file_data()装饰器会从json或yaml中加载数据。只有以“.yml” 和 “.yaml” 结尾的文件被加载为Yaml文件。所有其他格式文件都作为json文件加载,比如txt。

3.1 json
test.json

{
    "case1": {
        "a": 1,
        "b": 1,
		"c": 2
    },
    "case2": {
        "a": -1,
        "b": 1,
		"c": 0
    },
    "case3": {
        "a": 0,
        "b": 0,
		"c": 0
    }
}

示例

@ddt
class MyTest(unittest.TestCase):
    @file_data("test.json")
    def test(self,a,b,c):
        self.assertEqual(add(a, b),c)

命名:测试用例方法+key
# ok test_00001_case1 (TS001.MyTest)
# ok test_00002_case2 (TS001.MyTest)
# ok test_00003_case3 (TS001.MyTest)

3.2 多层级json
test.json

{
    "case1": {
        "data": {
            "a": 1,
            "b": 1
        },
        "result": 2
    },
    "case2": {
        "data": {
            "a": 0,
            "b": 1
        },
        "result": 1
    },
    "case3": {
        "data": {
            "a": 0,
            "b": 0
        },
        "result": 0
    }
}

示例

@ddt
class MyTest(unittest.TestCase):
    @file_data("test.json")
    def test(self,data,result):
        self.assertEqual(add(data), result)
命名:测试用例方法+key
ok test_00001_case1 (TS001.MyTest)
ok test_00002_case2 (TS001.MyTest)
ok test_00003_case3 (TS001.MyTest)

3.2 yml 需要安装yml(pip install PyYAML)
test.yml
python+unittest+ddt实现数据驱动测试_第2张图片

def add(a,b):
    return a+b

@ddt
class MyTest(unittest.TestCase):
    @file_data("test.yml")
    def test(self,a,b,c):
        self.assertEqual(add(a, b),c)

ok test_00001_case1 (TS001.MyTest)
ok test_00002_case2 (TS001.MyTest)
ok test_00003_case3 (TS001.MyTest)

你可能感兴趣的:(接口测试,装饰器,unittest,python,单元测试)