3、httprunner基本用法

俊先 2022-01-07 08:17:23 阅读数:481

httprunner 基本 用法

1、.env文件

base_url,配置信息等可以寫在裏面然後用 ${ENV()} 引用

在這裏插入圖片描述

login.yml

config:
name: 登錄幕布
variables: {
}
verify: false
teststeps:
- name: /v3/api/user/phone_login
request:
headers:
accept: application/json, text/plain, */* accept-encoding: gzip, deflate, br accept-language: zh-CN,zh;q=0.9 content-length: '69' content-type: application/json;charset=UTF-8 data-unique-id: 7b70bf17-50f2-470e-ae20-ecbbd0d6190f jwt-token: '' origin: https://mubu.com referer: https://mubu.com/ sec-ch-ua: '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"' sec-ch-ua-mobile: ?0 sec-ch-ua-platform: '"Windows"' sec-fetch-dest: empty sec-fetch-mode: cors sec-fetch-site: same-site user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 version: 3.0.0-2.0.0.1824 x-request-id: 8a061286-b850-40a2-b680-6a0314367fed json: callbackType: 0 password: ${ENV(PASSWORD)} phone: ${ENV(PHONE)} method: POST url: https://api2.mubu.com/v3/api/user/phone_login validate: - eq: - status_code - 200 - eq: - body.code - 0 

2、debugtalk.py

可以利用debugtalk.py寫動態計算的方法(類似pytest中的conftest.py)等,然後直接用 ${
函數名()} 調用

debugtalk.py

import time
import os
from httprunner import __version__
from testcases.login_test import TestCaseLogin
path = os.path.dirname(__file__) + '/cookies'
def get_httprunner_version():
return __version__
def sum_two(m, n):
return m + n
def sleep(n_secs):
time.sleep(n_secs)
def get_token():
if (os.path.exists(path)):
with open(path,mode='r',encoding='utf-8') as f:
token=f.read()
print(f'獲取的token為:{token}')
return token
else:
with open(path,mode='w'):
TestCaseLogin().test_start()
print("生成token")
def set_token(login_response):
with open(path,'w') as f:
f.write(login_response.json['data']['token'])
def get_file_name():
return ['利用debugtalk創建的文件夾1','利用debugtalk創建的文件夾2']
if __name__ == '__main__':
# get_token()
print(os.path.dirname(__file__))

create_dir_test.py

# NOTE: Generated By HttpRunner v3.1.6
# FROM: har\create_dir.yml
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
#取別名,防止pytest 把導入的以Test開頭的用例也執行了
from testcases.login_test import TestCaseLogin as Login
class TestCaseCreateDir(HttpRunner):
config = Config("HttpRunner實現幕布接口-文件夾add操作")\
.verify(False).export(*['folder_id'])
teststeps = [
# Step(
# RunTestCase("運行前置用例-登錄")
# .call(Login)
# .export(*['token']) #1、運行Login用例,把token變量導出在當前用例使用,這裏不寫export的話,Login用例中的Config使用export也可
# ),
Step(
RunRequest("幕布-新增文件夾")
.post("https://api2.mubu.com/v3/api/list/create_folder")
.with_headers(
**{

"accept": "application/json, text/plain, */*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "zh-CN,zh;q=0.9",
"content-length": "33",
"content-type": "application/json;charset=UTF-8",
"data-unique-id": "7b70bf17-50f2-470e-ae20-ecbbd0d6190f",
"jwt-token": "${get_token()}",
"origin": "https://mubu.com",
"referer": "https://mubu.com/",
"sec-ch-ua": '"Google Chrome";v="95", "Chromium";v="95", ";Not A Brand";v="99"',
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": '"Windows"',
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-site",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36",
"version": "3.0.0-2.0.0.1824",
"x-request-id": "195dee55-182a-4eb7-a307-714fa3f72dd1",
}
)
.with_json({
"folderId": "0", "name": "測試toke通過文件傳遞22"})
.extract()
.with_jmespath('body.data.folder.id',"folder_id")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
),
]
if __name__ == "__main__":
TestCaseCreateDir().test_start()
版权声明:本文为[俊先]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201070817231974.html