YAML文件存放传给接口的数据(用例),可转变成python的字典以及list
YAML文件内容:
username : niuhanyang #可以转换成字典
passwd : 123456
port : 3306
databases : #转换成嵌套的字典
test : 192.168.1.2
dev : 192.168.1.3
pre : 192.168.1.5
tester : #可以转换成list
- 马佩佩
- 小傅
- 璐璐
处理yaml.py
# import yaml,json
# f = open('login.yml',encoding='utf-8')
# print(json.dumps(yaml.load(f),indent=3))
# import yaml
# f = open('abc.yml',encoding='utf-8')
# print(yaml.load(f))
import yaml,json
f = open('login.yml',encoding='utf-8')
print(json.dumps(yaml.load(f),indent=3))
ddt使用
import ddt #ddt是数据驱动的模块
import unittest,requests
import BeautifulReport
@ddt.ddt
class MyCase(unittest.TestCase):
# @ddt.data(1,2,3)
# def testa(self,num):
# self.assertTrue(num>2)
#
# @ddt.data([3,2],[4,3],[5,3])
# @ddt.unpack#将list分解,解包操作
# def testb(self,num,num2):
# self.assertTrue(num>num2)
@ddt.file_data('login.yml')
@ddt.unpack
def test_run(self,**kwargs): #一个*代表list,**代表字典
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:
requests.get(url,params=data,headers=header, cookies=cookie)#params是把参数拼到url后面
#self.assertEqual(check.get('error_code'),r.json().get('error_code'))
for c in check:
self.assertIn(c, r.text)
if __name__ == '__main__':
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(MyCase))
report_html = BeautifulReport.BeautifulReport(suite)
report_html.report(log_path='.',filename='my_report',description='用例描述')