pytest用yaml文件編寫測(cè)試用例流程詳解
前言
本篇來(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
到此這篇關(guān)于pytest用yaml文件編寫測(cè)試用例流程詳解的文章就介紹到這了,更多相關(guān)pytest編寫測(cè)試用例內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python pandas 時(shí)間日期的處理實(shí)現(xiàn)
這篇文章主要介紹了python pandas 時(shí)間日期的處理實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07人工智能學(xué)習(xí)pyTorch的ResNet殘差模塊示例詳解
這篇文章主要為大家介紹了人工智能學(xué)習(xí)pyTorch的ResNet殘差模塊示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步2021-11-11Python巧用SnowNLP實(shí)現(xiàn)生成srt字幕文件
SnowNLP是一個(gè)可以方便的處理中文文本內(nèi)容的python類庫(kù),本文主要為大家詳細(xì)介紹了Python如何巧用SnowNLP實(shí)現(xiàn)將一段話一鍵生成srt字幕文件,感興趣的可以了解下2024-01-01pytorch中實(shí)現(xiàn)彩色圖像(三通道)轉(zhuǎn)灰度圖像(單通道)
這篇文章主要介紹了pytorch中實(shí)現(xiàn)彩色圖像(三通道)轉(zhuǎn)灰度圖像(單通道),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02用xpath獲取指定標(biāo)簽下的所有text的實(shí)例
今天小編就為大家分享一篇用xpath獲取指定標(biāo)簽下的所有text的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01PyTorch的自適應(yīng)池化Adaptive Pooling實(shí)例
今天小編就為大家分享一篇PyTorch的自適應(yīng)池化Adaptive Pooling實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01解決Python3中Matplotlib繪圖顯示方塊的問(wèn)題
這篇文章主要介紹了解決Python3中Matplotlib繪圖顯示方塊的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09