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-03-15 04:20:53 +08:00
|
|
|
def pytest_collect_file(parent, fspath):
|
|
|
|
if fspath.suffix == ".yaml" and fspath.name.startswith("test"):
|
|
|
|
return YamlFile.from_parent(parent, path=fspath)
|
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):
|
2010-10-27 20:52:28 +08:00
|
|
|
def __init__(self, name, parent, spec):
|
2019-06-03 06:32:00 +08:00
|
|
|
super().__init__(name, parent)
|
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.",
|
|
|
|
]
|
|
|
|
)
|
2010-10-27 20:52:28 +08:00
|
|
|
|
|
|
|
def reportinfo(self):
|
2020-10-03 04:16:22 +08:00
|
|
|
return self.fspath, 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."""
|