1. 直接引用其它测试用例中的token
进行简单操作:登录系统,获取列表(需要传token等参数)。
脚本录制完成后,为了方便维护,可引入测试用例分层机制,将用例分为“1.登录”和“2.获取列表”。
testcases:
1.login.yml
2.get_list.yml
获取列表时,需要用例1返回的token值,因此,需要在测试用例2中首先调用用例1,进行登录操作。
具体脚本如下:
- login.yml
config:
name: testcase description
variables:
timestamp: ${get_timestamp()}
verify: False
#在别的测试用例中引用
export:
- get_token
teststeps:
- name: login system
request:
data:
_sign: bd49d5b78c71d2268c181e9c70ba9233
_timestamp: '1596009876000'
loginName: yourname
password: yourpassword
terminal: pc
token: ''
v: ''
headers:
Content-Type: application/x-www-form-urlencoded;charset=UTF-8
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
method: POST
url: http:******/login
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.code, 200]
extract:
#在接口返回字段中找到token
get_token: content.results.data.token
- get_list.yml
config:
name: testcase description
variables: {}
verify: False
export:
- get_industryKey
teststeps:
#在获取列表前,首先进行登录操作获取token
- name: login system
testcase: testcases/login.yml
#与login.yml中的export对应,可直接输出参数get_token
extract:
- get_token
- name: 获取列表
request:
headers:
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
method: GET
params:
_sign: aac75dd9eaf23a733e513b70b6f2e567
_timestamp: '1596608206000'
terminal: pc
token: ${get_token}
v: ''
url: http:*******/app_information/get_industry_list
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.code, 200]
- eq: [content.results.msg, "成功"]
测试报告如下:
2. 使用debugtalk.py
基本上每一步操作都需要token,如果将获取token操作放在debugtalk.py中,可以更有效的维护测试用例。
首先在debugtalk.py中,用python实现登录操作,然后将获取的cookie引用到需要的测试用例中。
具体代码如下:
- debugtalk.py
import requests
def get_cookie(username="yourname", password="yourpassword"):
url = "http:******/login"
headers = {
"Content-Type":"application/x-www-form-urlencoded;charset=UTF-8",
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36"
}
body = {
#登录接口需要的参数
"_sign": 'bd49d5b78c71d2268c181e9c70ba9233',
"_timestamp": '1596009876000',
"loginName": username,
"password": password,
"terminal":'pc',
"token": '',
"v": ''
}
res = requests.post(url, headers=headers, data=body)
try:
sign = res.json()["login_info"]["sign"]
except:
sign = ""
cookie = "{}={}".format(username, sign)
return cookie
if __name__ == '__main__':
cookie = get_cookie()
print(cookie)
- get_list.yml
config:
name: testcase description
variables:
#调用debugtalk.py的get_cookie()
get_cookie: ${get_cookie()}
verify: False
teststeps:
- name: 获取行业列表
request:
headers:
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
#cookie中包含需要的token等参数,放在请求头中
Cookie: ${get_cookie}
method: GET
#其它需要的参数可继续添加
#params:
# param1:***
url: http:******/get_industry_list
validate:
- eq: [status_code, 200]
- eq: [headers.Content-Type, application/json]
- eq: [content.code, 200]
- eq: [content.results.msg, "成功"]
测试报告中如下: