1 Star 1 Fork 7

小新/Interface_test_example

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
conftest.py 2.77 KB
一键复制 编辑 原始数据 按行查看 历史
随风挥手 提交于 2022-05-08 18:30 . update
# -*- coding: utf-8 -*-
__author__ = 'wxhou'
__email__ = '1084502012@qq.com'
"""
pytest处理
"""
import typing as t
import yaml
import pytest
from requests import Response
from common.cache import cache
from common.json import dumps, loads
from common.request import HttpRequest
from common.regular import findalls, sub_var
from common.result import get_result, check_results
from common.exceptions import YamlException, RequestException
from utils.logger import logger
def pytest_collect_file(parent, path):
if path.ext in (".yaml", ".yml") and path.basename.startswith("test"):
return YamlFile.from_parent(parent, fspath=path)
def pytest_collection_modifyitems(items):
for item in items:
item.name = item.name.encode('unicode_escape').decode('utf-8')
item._nodeid = item.nodeid.encode('unicode_escape').decode('utf-8')
class YamlFile(pytest.File):
def collect(self):
raw = yaml.safe_load(self.fspath.open(encoding='utf-8'))
if not any(k.startswith('test') for k in raw.keys()):
raise YamlException("{}yaml non test found".format(self.fspath))
if variable := raw.get('variable'):
for k, v in variable.items():
cache.set(k, v)
if config := raw.get('config'):
keys = findalls(dumps(config))
config = loads(sub_var({k: cache.get(k)
for k in keys}, dumps(config)))
for k, v in config.items():
cache.set(k, v)
if tests := raw.get('tests'):
for name, spec in tests.items():
yield YamlTest.from_parent(self,
name=spec.get(
'description') or name,
spec=spec)
class YamlTest(pytest.Item):
def __init__(self, name, parent, spec):
super(YamlTest, self).__init__(name, parent)
self.spec = spec
self.request = HttpRequest(exception=(RequestException, Exception))
def runtest(self):
"""Some custom test execution (dumb example follows)."""
r = self.request.send_request(**self.spec)
self.response_handle(r, self.spec.get(
'Validate'), self.spec.get('Extract'))
def response_handle(self, r: Response, validate: t.Dict, extract: t.List):
"""Handling of responses"""
if validate:
check_results(r, validate)
if extract:
get_result(r, extract)
def repr_failure(self, excinfo):
"""Called when self.runtest() raises an exception."""
logger.critical(excinfo.value)
logger.critical(excinfo.traceback[-6:-1])
def reportinfo(self):
return self.fspath, 0, f"usecase: {self.name}"
if __name__ == '__main__':
pytest.main()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xiaoxinai/interface_test_example.git
git@gitee.com:xiaoxinai/interface_test_example.git
xiaoxinai
interface_test_example
Interface_test_example
master

搜索帮助