Httprunner框架学习--辅助函数debugtalk.py
程序员文章站
2024-03-21 23:10:04
...
Httprunner框架中,使用yaml或json文件进行用例描述,无法做一些复杂操作,如保存一些数据跨文件调用,或者实现一些复杂逻辑判断等,为了解决这个问题,引入了debugtalk.py辅助函数来进行一些复杂操作。
使用步骤:
1.新建debugtalk.py,在这个py文件中编写你要实现的逻辑代码函数(要与yaml文件同目录下)
2.在yaml或json中引用对应的函数
eg:
debugtalk.py
import requests
import json
host = "http://127.0.0.1:5000"
#获取token
def get_token():
headers = {
'Content-Type': 'application/json',
# 'User - Agent': 'python - requests/2.18.4',
'app_version': '2.8.6',
'device_sn': 'FwgRiO7CNA50DSU',
'os_platform': 'ios'
}
datas = {'sign': '9c0c7e51c91ae963c833a4ccbab8d683c4a90c98'}
url = "http://127.0.0.1:5000/api/get-token"
response = requests.post(url=url, json=datas, headers=headers)
print(type(response), response.json()["token"])
return response.json()["token"]
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000
parameters:
# - uid: ${parameterize(uid.csv)}
- uid: ${P(uid.csv)} #简写方式
variables:
username: user1
password: 123456
# token: Token ${get_token()}
#- test:
# name: /api/get-token
# request:
# headers:
# Content-Type: application/json
# User-Agent: python-requests/2.18.4
# app_version: 2.8.6
# device_sn: FwgRiO7CNA50DSU
# os_platform: ios
# json:
# sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
# method: POST
# url: /api/get-token
# validate:
# - eq: [status_code, 200]
# - eq: [headers.Content-Type, application/json]
# - eq: [content.success, true]
# # - eq: [content.token, baNLX1zhFYP11Seb]
# extract:
# - token: content.token
- test:
name: /api/users/$uid
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: ${get_token()} #引用方法
json:
name: $username
password: $password
method: POST
url: /api/users/$uid
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
结果:
2.hook机制:相当于unittest中的setup,teardown函数,同样的也存在全局的setup,teardown和每一个teststep的setup,teardown。
使用步骤:
1.新建debugtalk.py,在这个py文件中编写你要实现的逻辑代码函数(要与yaml文件同目录下)
2.在yaml或json中引用对应的函数,键名为setup_hooks,teardown_hooks
eg:
debugtalk.py
#!usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
host = "http://127.0.0.1:5000"
def get_token():
headers = {
'Content-Type': 'application/json',
# 'User - Agent': 'python - requests/2.18.4',
'app_version': '2.8.6',
'device_sn': 'FwgRiO7CNA50DSU',
'os_platform': 'ios'
}
datas = {'sign': '9c0c7e51c91ae963c833a4ccbab8d683c4a90c98'}
url = "http://127.0.0.1:5000/api/get-token"
response = requests.post(url=url, json=datas, headers=headers)
print(type(response), response.json()["token"])
return response.json()["token"]
def setup():
print("setup开始了")
def tear_down():
print("teardown结束啦")
def hooks(s):
print(s)
- config:
name: testcase description
request:
base_url: http://127.0.0.1:5000
parameters:
# - uid: ${parameterize(uid.csv)}
- uid: ${P(uid.csv)} #简写方式
variables:
username: user1
password: 123456
setup_hooks:
- ${setup()}
teardown_hooks:
- ${tear_down()}
# token: Token ${get_token()}
#- test:
# name: /api/get-token
# request:
# headers:
# Content-Type: application/json
# User-Agent: python-requests/2.18.4
# app_version: 2.8.6
# device_sn: FwgRiO7CNA50DSU
# os_platform: ios
# json:
# sign: 9c0c7e51c91ae963c833a4ccbab8d683c4a90c98
# method: POST
# url: /api/get-token
# validate:
# - eq: [status_code, 200]
# - eq: [headers.Content-Type, application/json]
# - eq: [content.success, true]
# # - eq: [content.token, baNLX1zhFYP11Seb]
# extract:
# - token: content.token
- test:
name: /api/users/$uid
request:
headers:
Content-Type: application/json
User-Agent: python-requests/2.18.4
device_sn: FwgRiO7CNA50DSU
token: ${get_token()}
json:
name: $username
password: $password
method: POST
url: /api/users/$uid
validate:
- eq: [status_code, 201]
- eq: [headers.Content-Type, application/json]
- eq: [content.success, true]
- eq: [content.msg, user created successfully.]
setup_hooks:
- ${hooks("setup")}
teardown_hooks:
- ${hooks("teardown")}
运行结果:
上一篇: postman接口测试七
推荐阅读
-
Httprunner框架学习--辅助函数debugtalk.py
-
CI框架验证码CAPTCHA辅助函数用法实例
-
tp框架写了个步骤获取*分类return结果就是null,在辅助函数都能打印出结果
-
CI框架教程之优化验证码机制详解【验证码辅助函数】
-
CI框架验证码CAPTCHA辅助函数用法实例
-
Laravel5框架添加自定义辅助函数的方法
-
[PHP]CodeIgniter学习手册(五):表单辅助函数Form的应用教程
-
tp框架写了个步骤获取*分类return结果就是null,在辅助函数都能打印出结果
-
分享淘宝API辅助函数-适用CI框架
-
tp框架写了个步骤获取*分类return结果就是null,在辅助函数都能打印出结果