2010-10-27 20:52:28 +08:00
|
|
|
# content of conftest.py
|
2010-11-13 18:10:45 +08:00
|
|
|
import pytest
|
2010-10-27 20:52:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2021-12-07 05:55:06 +08:00
|
|
|
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)
|
2012-11-01 00:00:55 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class YamlFile(pytest.File):
|
2010-10-27 20:52:28 +08:00
|
|
|
def collect(self):
|
2020-07-18 17:35:13 +08:00
|
|
|
# We need a yaml parser, e.g. PyYAML.
|
|
|
|
import yaml
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2021-03-15 04:20:53 +08:00
|
|
|
raw = yaml.safe_load(self.path.open())
|
2016-08-02 06:37:52 +08:00
|
|
|
for name, spec in sorted(raw.items()):
|
2019-11-23 08:08:56 +08:00
|
|
|
yield YamlItem.from_parent(self, name=name, spec=spec)
|
2010-10-27 20:52:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-13 16:05:11 +08:00
|
|
|
class YamlItem(pytest.Item):
|
2022-01-27 18:49:18 +08:00
|
|
|
def __init__(self, *, spec, **kwargs):
|
|
|
|
super().__init__(**kwargs)
|
2010-10-27 20:52:28 +08:00
|
|
|
self.spec = spec
|
2012-11-01 00:00:55 +08:00
|
|
|
|
2010-10-27 20:52:28 +08:00
|
|
|
def runtest(self):
|
2016-08-02 06:37:52 +08:00
|
|
|
for name, value in sorted(self.spec.items()):
|
2020-07-18 17:35:13 +08:00
|
|
|
# Some custom test execution (dumb example follows).
|
2010-10-27 20:52:28 +08:00
|
|
|
if name != value:
|
2010-11-06 06:37:25 +08:00
|
|
|
raise YamlException(self, name, value)
|
2010-10-27 20:52:28 +08:00
|
|
|
|
|
|
|
def repr_failure(self, excinfo):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Called when self.runtest() raises an exception."""
|
2010-11-06 06:37:25 +08:00
|
|
|
if isinstance(excinfo.value, YamlException):
|
2018-05-23 22:48:46 +08:00
|
|
|
return "\n".join(
|
|
|
|
[
|
|
|
|
"usecase execution failed",
|
2019-08-12 14:09:53 +08:00
|
|
|
" spec failed: {1!r}: {2!r}".format(*excinfo.value.args),
|
2018-05-23 22:48:46 +08:00
|
|
|
" no further details known at this point.",
|
|
|
|
]
|
|
|
|
)
|
2023-05-24 11:06:05 +08:00
|
|
|
return super().repr_failure(excinfo)
|
2010-10-27 20:52:28 +08:00
|
|
|
|
|
|
|
def reportinfo(self):
|
2021-10-09 19:06:28 +08:00
|
|
|
return self.path, 0, f"usecase: {self.name}"
|
2010-10-27 20:52:28 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
|
2010-11-06 06:37:25 +08:00
|
|
|
class YamlException(Exception):
|
2020-07-18 17:35:13 +08:00
|
|
|
"""Custom exception for error reporting."""
|