前言
httprunner 用 yaml 文件实现接口自动化框架很好用,最近在看 pytest 框架,于是参考 httprunner的用例格式,写了一个差不多的 pytest 版的简易框架
项目结构设计
项目结构完全符合 pytest 的项目结构,pytest 是查找 test_.py 文件,我这里是查找 test_.yml 文件,唯一不同的就是这个地方
以前写test_*.py 的测试用例,现在完全不用写了,全部写yaml 文件就行,项目结构参考
只需在 conftest.py 即可实现,代码量超级少
pytest 7.x最新版
def pytest_collect_file(parent, file_path):
# 获取文件.yml 文件,匹配规则
if file_path.suffix == ".yml" and file_path.name.startswith("test"):
return YamlFile.from_parent(parent, path=file_path)
pytest 5.x以上版本
import pytest
import requests
def pytest_collect_file(parent, path):
# 获取文件.yml 文件,匹配规则
if path.ext == ".yml" and path.basename.startswith("test"):
# print(path)
# print(parent)
# return YamlFile(path, parent)
return YamlFile.from_parent(parent, fspath=path)
class YamlFile(pytest.File):
# 读取文件内容
def collect(self):
import yaml
raw = yaml.safe_load(self.fspath.open(encoding='utf-8'))
for yaml_case in raw:
name = yaml_case["test"]["name"]
values =