代码拉取完成,页面将自动刷新
同步操作将从 随风挥手/Interface_test_example 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# -*- 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()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。