test_ok2/doc/en/example/nonpython/conftest.py

48 lines
1.4 KiB
Python
Raw Normal View History

# content of conftest.py
import pytest
2018-05-23 22:48:46 +08:00
def pytest_collect_file(parent, fspath):
if fspath.suffix == ".yaml" and fspath.name.startswith("test"):
return YamlFile.from_parent(parent, path=fspath)
2018-05-23 22:48:46 +08:00
class YamlFile(pytest.File):
def collect(self):
# We need a yaml parser, e.g. PyYAML.
import yaml
2018-05-23 22:48:46 +08:00
raw = yaml.safe_load(self.path.open())
for name, spec in sorted(raw.items()):
2019-11-23 08:08:56 +08:00
yield YamlItem.from_parent(self, name=name, spec=spec)
2018-05-23 22:48:46 +08:00
class YamlItem(pytest.Item):
def __init__(self, name, parent, spec):
2019-06-03 06:32:00 +08:00
super().__init__(name, parent)
self.spec = spec
def runtest(self):
for name, value in sorted(self.spec.items()):
# Some custom test execution (dumb example follows).
if name != value:
2010-11-06 06:37:25 +08:00
raise YamlException(self, name, value)
def repr_failure(self, excinfo):
"""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.",
]
)
def reportinfo(self):
2020-10-03 04:16:22 +08:00
return self.fspath, 0, f"usecase: {self.name}"
2018-05-23 22:48:46 +08:00
2010-11-06 06:37:25 +08:00
class YamlException(Exception):
"""Custom exception for error reporting."""