Data Driven Testing,数据驱动,简单来说就是测试数据的参数化
Python数据驱动模块DDT,包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据)
通常情况下,data中的数据按照一个参数传递给测试用例,如果data中含有多个数据,以元组,列表,字典等数据,需要自行在脚本中对数据进行分解或者使用unpack分解数据。
@data(*参数)
拆分参数为多个,运行多次用例
@data(a,b)
那么a和b各运行一次用例
@data([a,d],[c,d])
如果没有@unpack,那么[a,b]当成一个参数传入用例运行
如果有@unpack,那么[a,b]被分解开,按照用例中的两个参数传递
pip install ddt
import requests,json,unittest
from ddt import ddt,data,unpack
@ddt
@data(参数)
实验证明:
1、@data可传列表、元祖、字典
2、@data(参数)时,是把参数当做一个变量传入处理的
#data是元祖
import requests,json,unittest
from ddt import ddt,data,unpack
@ddt
class Test(unittest.TestCase):
name=(('橙子','橘子'),('柠檬','柚子'))
@data(name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
(('橙子', '橘子'), ('柠檬', '柚子'))
Process finished with exit code 0
#data是列表
@ddt
class Test(unittest.TestCase):
name=[['橙子','橘子'],['柠檬','柚子']]
@data(name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
[['橙子', '橘子'], ['柠檬', '柚子']]
.
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Process finished with exit code 0
#data是字典
@ddt
class Test(unittest.TestCase):
name={'name':'王荔','age':11}
@data(name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
.
{'name': '王荔', 'age': 11}
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK
Process finished with exit code 0
@ddt
@data(*参数)
实验证明:
1、@data可传列表、元祖、字典
2、@data(*参数)时,是把参数拆分成多个迭代次数
import requests,json,unittest
from ddt import ddt,data,unpack
@ddt
class Test(unittest.TestCase):
name=(('橙子','橘子'),('柠檬','柚子'))
@data(*name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
..
('橙子', '橘子')
----------------------------------------------------------------------
('柠檬', '柚子')
Ran 2 tests in 0.000s
OK
Process finished with exit code 0
@ddt
class Test(unittest.TestCase):
name=[['橙子','橘子'],['柠檬','柚子']]
@data(*name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
..
['橙子', '橘子']
----------------------------------------------------------------------
['柠檬', '柚子']
Ran 2 tests in 0.000s
OK
Process finished with exit code 0
@ddt
class Test(unittest.TestCase):
name=[{'name':'王荔','age':11},{'name':'王荔','age':11}]
@data(*name)
def test(self,name):
print(name)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
..
{'age': 11, 'name': '王荔'}
{'age': 11, 'name': '王荔'}
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
Process finished with exit code 0
@ddt
@data(*参数)
@unpack
实验证明:
1、@data可传列表、元祖、字典
2、@data(*参数)时,是把参数拆分成多个迭代次数
3、@unpack 是把参数拆分成多个参数传入
import requests,json,unittest
from ddt import ddt,data,unpack
@ddt
class Test(unittest.TestCase):
name=(('橙子','橘子'),('柠檬','柚子'))
@data(*name)
@unpack
def test(self,name1,name2):
print(name1,name2)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
橙子 橘子
柠檬 柚子
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK
Process finished with exit code 0
@ddt
class Test(unittest.TestCase):
name=[['橙子','橘子'],['柠檬','柚子']]
@data(*name)
@unpack
def test(self,name1,name2):
print(name1,name2)
if __name__=='__main__':
unittest.main()
"C:\Program Files\Python35\python.exe" C:/Users/wangli/PycharmProjects/Test/Test/test.py
..
橙子 橘子
----------------------------------------------------------------------
Ran 2 tests in 0.001s
OK
柠檬 柚子
Process finished with exit code 0