4、httprunner參數化

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

httprunner

一、parameters參數

運行次數是笛卡爾積的計算方式,如下將運行2*2=4次

yml方式

config:
name: 登錄幕布
variables: {
}
parameters:
phone: ['13104978922','13722349423']
password: ['123456','2345']
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: $password phone: $phone method: POST url: https://api2.mubu.com/v3/api/user/phone_login validate: - eq: - status_code - 200 - eq: - body.code - 0 

py文件方式

# NOTE: Generated By HttpRunner v3.1.6
# FROM: testcases\ddt\create_dir.yml
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseCreateDir(HttpRunner):
@pytest.mark.parametrize(
"param", Parameters({
"newfile": ["新增文件夾1", "新增文件夾2", "新增文件夾3"]})
)
def test_start(self, param):
super().test_start(param)
config = Config("HttpRunner實現幕布接口增删改查").verify(False)
teststeps = [
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": "$newfile"})
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
),
]
if __name__ == "__main__":
TestCaseCreateDir().test_start()

二、debugtalk.py方式

直接使用${函數名()} 調用

# NOTE: Generated By HttpRunner v3.1.6
# FROM: har\create_dir.yml
from typing import Dict
import pytest
from httprunner import HttpRunner, Config, Step, RunRequest, Parameters
#取別名,防止pytest 把導入的以Test開頭的用例也執行了
from testcases.login_test import TestCaseLogin as Login
class TestCaseCreateDirDeTalk(HttpRunner):
@pytest.mark.parametrize(
"param", Parameters({
"filename": "${get_file_name()}"})
)
def test_start(self, param):
super().test_start(param)
config = Config("HttpRunner實現幕布接口-文件夾add操作")\
.verify(False).export(*['folder_id','param'])
teststeps = [
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": "$filename"})
.extract()
.with_jmespath('body.data.folder.id',"folder_id")
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
),
]
if __name__ == "__main__":
TestCaseCreateDirDeTalk().test_start()

三、CSV方式

1、新建文件 testdata/userpwd.csv

phone,password
13822228765,12312
13822508211,baba139262265
13822508676,baba139262144

2、yml方式

config:
name: 登錄幕布
variables: {
}
parameters:
phone-password: ${
P(testdata/userpwd.csv)}
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: $password phone: $phone method: POST url: https://api2.mubu.com/v3/api/user/phone_login validate: - eq: - status_code - 200 - eq: - body.code - 0 

2、py文件方式

# NOTE: Generated By HttpRunner v3.1.6
# FROM: testcases\ddt\login_ddt_csv.yml
import pytest
from httprunner import Parameters
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
class TestCaseLoginDdtCsv(HttpRunner):
@pytest.mark.parametrize(
"param", Parameters({
"phone-password": "${P(testdata/userpwd.csv)}"})
)
def test_start(self, param):
super().test_start(param)
config = Config("登錄幕布").verify(False)
teststeps = [
Step(
RunRequest("/v3/api/user/phone_login")
.post("https://api2.mubu.com/v3/api/user/phone_login")
.with_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",
}
)
.with_json({
"callbackType": 0, "password": "$password", "phone": "$phone"})
.validate()
.assert_equal("status_code", 200)
.assert_equal("body.code", 0)
),
]
if __name__ == "__main__":
TestCaseLoginDdtCsv().test_start()
版权声明:本文为[俊先]所创,转载请带上原文链接,感谢。 https://gsmany.com/2022/01/202201070817232355.html