python ddt file_data

# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: yml_test
Description :
Author : Administrator
date: 2019/6/29 0029
-------------------------------------------------
"""
import ddt, yaml
import unittest, requests

f = open(r'data.yaml', encoding='utf-8')
res = yaml.load(f)
print(type(res))
print(res)


@ddt.ddt
class MyCase(unittest.TestCase):
@ddt.file_data('data.yaml')
@ddt.unpack
def test_run(self, **kwargs):
method = kwargs.get('method')
url = kwargs.get('url')
data = kwargs.get('data', {})
header = kwargs.get('header', {})
is_json = kwargs.get('is_json', 0)
cookie = kwargs.get('cookie', {})
check = kwargs.get('check')
if method == 'post':
if is_json:
r = requests.post(url, json=data, headers=header, cookies=cookie)
else:
r = requests.post(url, data=data, headers=header, cookies=cookie)
else:
r = requests.get(url, params=data, header=header, cookies=cookie)
# self.assertEqual(check.get('error_code'),r.json().get('error_code'))
'''当校验很多的时候,需要用到循环'''
for c in check:
self.assertIn(c, r.text)

@ddt.file_data('baidu.yaml')
@ddt.unpack
def test_run_baidu(self, **kwargs):
method = kwargs.get('method')
url = kwargs.get('url')
check = kwargs.get('check')
if method == "get":
r = requests.get(url)
# self.assertEqual(check.get('error_code'),r.json().get('error_code'))
'''当校验很多的时候,需要用到循环'''
self.assertEqual(check["status_code"],r.status_code)


if __name__ == '__main__':
unittest.main()
posted @ 2019-06-30 08:10  tny_leyon  阅读(616)  评论(0编辑  收藏  举报