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

49 lines
1.5 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, file_path):
if file_path.suffix == ".yaml" and file_path.name.startswith("test"):
return YamlFile.from_parent(parent, path=file_path)
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, *, spec, **kwargs):
super().__init__(**kwargs)
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.",
]
)
return super().repr_failure(excinfo)
def reportinfo(self):
return self.path, 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."""