1103_ddt 数据处理
1、传入一个参数
1 import unittest 2 from ddt import ddt,data,unpack 3 4 @ddt 5 class MyTestCase1(unittest.TestCase): 6 @data(1,2,3) 7 def test_normal(self,value): 8 print(value) 9 self.assertEqual(value,2) 10 if __name__=='__main__':
2、传入元组、字典、列表等复杂结构数据,@data 标签结合 @unpack使用
import unittest from ddt import ddt,data,unpack @ddt class MyTestCase2(unittest.TestCase): @data((1,2),(2,3)) @unpack def test_tuple(self,value1,value2): print(value1,value2) self.assertEqual(value2,value1+1) @data([1,2],[2,3]) @unpack def test_list(self,value1,value2): print(value1,value2) self.assertEqual(value2,value1+1) @data({'value1':1,'value2':2},{'value1':1,'value2':2}) @unpack def test_dict(self,value1,value2): print(value1,value2) self.assertEqual(value2,value1+1) if __name__=='__main__':
3、以文件作为数据传递@file_data
传递json数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import unittest from ddt import ddt,data,unpack,file_data @ddt class MyTestCase3(unittest.TestCase): @file_data('E:\\a.json') def test_file(self,value): print(value) if __name__=='__main__': unittest.main()
以上只是简单的使用了ddt模块进行数据驱动,实际项目应用过程中应该还需要更多知识的结合使用。
https://blog.csdn.net/u013118036/article/details/45558951
http://www.cnblogs.com/nuonuozhou/p/8645129.html
这篇文章将ddt模块与xldr excel一起使用,进行数据驱动
自己的写的的
class_011.py
import unittest from ddt import ddt,data from API_AUTO.http_requests import HttpRequest from class_1103.read_me import GetData from class_1103.gyp1101 import datedd test_data=datedd('xg.xlsx','python1').get_data() @ddt class TestLogin(unittest.TestCase): def setUp(self): pass #正常登录 @data(*test_data) def test_api(self,item): res = HttpRequest().http_requests(item['http_method'],item['url'],eval(item['data']),getattr(GetData,'Cookie')) if res.cookies: #如果cookie有的话,那么就更新COOKIE setattr(GetData,'Cookie',res.cookies) try: self.assertIn(str(item['expected']),res.json()['code']) except AssertionError as e: print('登录出错了%s' %e) raise return res def tearDown(self): pass if __name__ == '__main__': unittest.main()
class_022.py
import unittest import HTMLTestRunner from class_1103.class_011 import TestLogin #具体到类名 from class_1101.gyp1101 import datedd #存储 suite = unittest.TestSuite() #加载 loader = unittest.TestLoader() #ddt不能用创建实力 suite.addTest(loader.loadTestsFromTestCase(TestLogin)) #输出 with open('test_report.html','wb') as file: runner = HTMLTestRunner.HTMLTestRunner(stream=file,verbosity=2,title='框架学习练习',tester='GYP测试') runner.run(suite)