python之數據驅動ddt


下載ddt並安裝

Pip install ddt

或者官網下載安裝

http://ddt.readthedocs.io/en/latest/

https://github.com/txels/ddt

DDT的使用

DDT包含類的裝飾器ddt和兩個方法裝飾器data(直接輸入測試數據),file_data(可以從json或者yaml中獲取測試數據)

只有yaml和yml結尾的文件以yaml形式上傳,其他情況下默認為json

通常情況下,data中的數據按照一個參數傳遞給測試用例,如果data中含有多個數據,以元組,列表,字典等數據,需要自行在腳本中對數據進行分解或者使用unpack分解數據

@data(a,b)

那么a和b各運行一次用例

@data([a,d],[c,d])

如果沒有unpack,那么[a,b]當成一個參數傳入用例運行

如果有unpack,那么[a,b]被分解開,按照用例中的兩個參數傳遞

@file_data(filename)

對於json的文件,每一個json元素按照一個用例運行,可以依照python分解元組,列表或者字典的方式分解傳入

import unittest
from ddt import ddt,data,file_data,unpack

@ddt
class demotest(unittest.TestCase):
def setup(self):
print "this is the setup"

@data(2,3)
def testb(self,value):
print value
print "this is test b"

@data([2,3],[4,5])
def testa(self,value):
print value
print "this is test a"

@data([2, 3], [4, 5])
@unpack
def testc(self, first,second):
print first
print second
print "this is test c"

@file_data('d:/data_dic.json')
def test_dic(self,value):
print value
print 'this is dic'

@file_data('d:/data.yml')
def test_yml(self, value):
print value
print 'this is yml'

def teardown(self):
print "this is the down"

if __name__ == '__main__':
unittest.main()
#suite=unittest.TestLoader.getTestCaseNames(demotest)
#suite = unittest.TestLoader().loadTestsFromTestCase(demotest)
#unittest.TextTestRunner(verbosity=2).run(suite)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM