網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
前言
本篇來(lái)學(xué)習(xí)在pytest中使用yaml編寫測(cè)試用例
項(xiàng)目結(jié)構(gòu)
conftest.py
只需在 conftest.py 即可實(shí)現(xiàn)使用yaml編寫測(cè)試用例
# -*- coding: utf-8 -*-
import jsonpath
import pytest
import requests
def pytest_collect_file(parent, file_path):
if file_path.suffix == ".yaml" and file_path.name.startswith("test"):
return YamlFile.from_parent(parent, path=file_path)
class YamlFile(pytest.File):
def collect(self):
import yaml
yml_raw = self.fspath.open(encoding='utf-8').read()
print('yml_raw', yml_raw)
yml_var = Template(yml_raw).safe_substitute(os.environ)
raw = yaml.safe_load(yml_var)
print('raw', raw)
for yaml_case in raw:
name = yaml_case["test"]["name"]
values = yaml_case["test"]
yield YamlItem.from_parent(self, name=name, spec=values)
class YamlItem(pytest.Item):
def __init__(self, name, parent, spec):
super().__init__(name, parent)
self.name = name
self.values = spec
self.request = self.values.get("request")
self.validate = self.values.get("validate")
self.s = requests.session()
def values_render_variable(self, values):
# 替換測(cè)試用例中的關(guān)聯(lián)值
yaml_test = Template(json.dumps(values)).safe_substitute(os.environ)
values = yaml.safe_load(yaml_test)
print('values', values)
return values
def runtest(self):
values = self.values_render_variable(self.values)
print('values:', values)
request_data = values["request"]
response = self.s.request(**request_data)
print("響應(yīng)數(shù)據(jù):", response.text)
# 判斷是否有extract提取參數(shù),實(shí)現(xiàn)參數(shù)關(guān)聯(lián)
if values.get("extract"):
for key, value in values.get("extract").items():
os.environ[key] = jsonpath.jsonpath(response.json(), value)[0]
print('key', key)
print('value', jsonpath.jsonpath(response.json(), value)[0])
# 斷言
print('validate:', self.validate)
self.assert_response(response, self.validate)
def assert_response(self, response, validate):
"""自定義斷言"""
for i in validate:
if "eq" in i.keys():
yaml_result = i.get("eq")[0]
actual_result = jsonpath.jsonpath(response.json(), yaml_result)
expect_result = i.get("eq")[1]
print("實(shí)際結(jié)果:%s" % actual_result[0])
print("期望結(jié)果:%s" % expect_result)
assert actual_result[0] == expect_result
yaml文件
test_method.yaml
說(shuō)明:
此yaml支持參數(shù)化
extract :提取關(guān)鍵字
- name: 后面引用變量的key值
- $.args.name:jsonpath 提取變量表達(dá)式
引用變量
- $name : $key
- test:
? ? name: get case
? ? request:
? ? ? url: https://postman-echo.com/get
? ? ? method: GET
? ? ? headers:
? ? ? ? Content-Type: application/json
? ? ? ? User-Agent: python-requests/2.18.4
? ? ? params:
? ? ? ? name: DH
? ? ? ? city: Beijing
? ? extract:
? ? ? name: $.args.name
? ? validate:
? ? ? - eq: [$.args.name, DH]
? ? ? - eq: [$.args.city, Beijing]
- test:
? ? name: post case
? ? request:
? ? ? url: https://postman-echo.com/post
? ? ? method: POST
? ? ? headers:
? ? ? ? Content-Type: application/json
? ? ? ? User-Agent: python-requests/2.18.4
? ? ? json:
? ? ? ? name: $name
? ? ? ? city: Beijing
? ? validate:
? ? ? - eq: [$.json.name, DH]
? ? ? - eq: [$.json.city, Beijing]
執(zhí)行并查看結(jié)果
pytest -s -v
原文鏈接:https://blog.csdn.net/IT_heima/article/details/127297186
相關(guān)推薦
- 2022-05-15 C++11:lambda表達(dá)式詳細(xì)介紹
- 2022-08-12 Python使用Opencv打開(kāi)筆記本電腦攝像頭報(bào)錯(cuò)解問(wèn)題及解決_python
- 2022-12-25 React?redux?原理及使用詳解_React
- 2022-11-01 Python正則表達(dá)中re模塊的使用_python
- 2022-07-07 ASP.Net使用System.Security.Principal模擬用戶_實(shí)用技巧
- 2022-09-24 ASP.NET?MVC實(shí)現(xiàn)多選下拉框_實(shí)用技巧
- 2022-04-10 Blazor實(shí)現(xiàn)數(shù)據(jù)驗(yàn)證_基礎(chǔ)應(yīng)用
- 2022-10-15 C++算法實(shí)現(xiàn)leetcode?1252奇數(shù)值單元格數(shù)目_C 語(yǔ)言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支