学习httprunner记录02
使用测试脚本完成文件的自动上传,以下脚本是使用requests实现的:
使用的环境
python3.6
pipenv 虚拟化环境
测试服务器是禅道,功能场景是提交bug添加附件(可以是图片、文本,压缩包等等)
# coding:utf-8
import requests
host = 'http://192.168.75.175'
h1 = {
"Accept": "application/json, text/javascript, */*; q=0.01",
"X-Requested-With": "XMLHttpRequest",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Accept-Encoding": "gzip, deflate",
"Referer": host + "/zentao/user-login.html",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
"Connection": "keep-alive"
}
url = host + '/zentao/user-login.html'
body = {
'account': 'admin',
'password': 'qazwsx123',
'referer': host+ '/zentao/',
'keepLogin': 0
}
s = requests.session()
r = s.post(url, data=body, headers=h1)
# print(r.content.decode('utf-8'))
# cookies = requests.utils.dict_from_cookiejar(r.cookies)
#
# for key in cookies.keys():
# if key == 'zentaosid':
# c = cookies.get(key)
# print(c)
# files = {
# 'files': open(r'/Users/wdy/Downloads/py3env/query_IP.json', 'rb'),
# 'Content-Disposition': 'form-data',
# 'Content-Type': 'application/json',
# 'filename': 'query_IP.json'
#
# }
files = {
'files': open(r'load.txt', 'rb'),
}
data = {
"color": "",
"title": "test file upload",
"steps": "<p>[步骤]</p>\n<br />\n<p>[结果]</p>\n<br />\n<p>[期望]</p>\n<br />",
"comment": "hello world",
"labels[]": "load.txt",
"files[]": "load.txt",
"uid": "5ef2ace7661f4",
"product": "1",
"module": "0",
"plan": "",
"type": "codeerror",
"severity": "3",
"pri": "3",
"status": "active",
"assignedTo": "admin",
"os": "osx",
"keywords": "",
"mailto[]": "",
"project": "",
"task": "",
"openedBuild[]": "trunk",
"resolvedBy": "",
"resolvedDate": "",
"resolvedBuild": "",
"resolution": "",
"duplicateBug": "0",
"closedBy": "",
"closedDate": ""
}
headers = {
"Host": "192.168.75.175",
"Upgrade-Insecure-Requests": "1",
"Origin": "http://192.168.75.175",
#"Content-Type": "multipart/form-data; boundary=----WebKitFormBoundaryvVlF9qeZZGCBZnBK",# 不需要设置,requests能够根据传递的参数自动识别
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Referer": "http://192.168.75.175/zentao/bug-edit-1.html",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8,lb;q=0.7",
"Pragma": "no-cache",
"Cache-Control": "no-cache"
}
upload_url = "http://192.168.75.175/zentao/bug-edit-2.html"
ur = s.post(upload_url, files=files, data=data, headers=headers)
print(ur.status_code)
print(ur.content)
以下脚本是使用httprunner3.x版本,先录制再手动修改完成,以便于和requests脚本相对比;
根据httprunner官方文档,接口调用部分是借助requests实现的,所以脚本表现形式有所差异,
但实质过程是一致的;
运行httprunner过程中发现两个bug,暂时先不把脚本贴出来了

浙公网安备 33010602011771号