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