DDT包含类的装饰器ddt和两个方法装饰器data(直接输入测试数据),file_data(可以从json或者yaml中获取测试数据)
实例代码:
import ddtimport unittesttest_data1 = [{ "username": "zhangsan", "pwd": "zhangsan"}, { "username": "lisi", "pwd": "lisi"}, { "username": "wangwu", "pwd": "wangwu"}, ]test_data2 = [{ "username": "wukong", "pwd": "wukong"}, { "username": "wuneng", "pwd": "woneng"}, { "username": "wujing", "pwd": "wujing"}, ]@ddt.ddtclass Test(unittest.TestCase): def setUp(self): print("Start!") def tearDown(self): print("end!") @ddt.data(*test_data1) def test_ddt1(self, data): print(data) @ddt.data(*test_data2) def test_ddt2(self, data): print(data)if __name__ == "__main__": unittest.main()
运行结果
接口测试使用:
import requests,ddt,unittest,jsondata=[200,201,200,201]@ddt.ddtclass DoubanTest(unittest.TestCase): def setUp(self): pass def tearDown(self): pass def beij(self): cliner=requests.get('https://api.douban.com/v2/user/q') m=cliner.status_code return m @ddt.data(*data) def test1(self,data): m=self.beij() print(data) self.assertEqual(m,data)if __name__ =='__main__': unittest.main()
运行示范
示范的是@data 这个装饰器的以上。
下面我们来看看文件的
@file_data(filename)
import ddtimport unittest@ddt.ddtclass Test(unittest.TestCase): def setUp(self): print("Start!") def tearDown(self): print("end!") @ddt.file_data('data.yaml') def test_ddt1(self, value): print(value)if __name__ == "__main__": unittest.main()
运行结果
支持json yaml文件类型