日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

pytest用yaml文件編寫測(cè)試用例流程詳解_python

作者:愛(ài)學(xué)習(xí)de測(cè)試小白 ? 更新時(shí)間: 2023-01-17 編程語(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

欄目分類
最近更新