前言
上一篇通过用例收集钩子 pytest_collect_file 把 yaml 文件收集起来的,仅仅只是收集到用例,还不能执行。
接下来详细讲解,如何把yaml 文件的内容,转成Item 用例去执行。
pytest_collect_file 收集钩子
准备一个待执行的YAML文件内容test_login.yml
name: login case1
request:
url: http://127.0.0.1:8000/api/v1/login/
method: POST
headers:
Content-Type: application/json
json:
username: test
password: 123456
先在conftest.py 写收集钩子
def pytest_collect_file(file_path: Path, parent):
# 获取文件.yml 文件,匹配规则
if file_path.suffix == ".yml" and file_path.name.startswith("test"):
return pytest.File.from_parent(parent, path=file_path)
如果收集到yaml 文件返回pytest.File.from_parent(parent, path=file_path)
,在运行的时候会出现报错
===================