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

47 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, path):
if path.ext == ".yaml" and path.basename.startswith("test"):
return YamlFile(path, parent)
2018-05-23 22:48:46 +08:00
class YamlFile(pytest.File):
def collect(self):
2018-05-23 22:48:46 +08:00
import yaml # we need a yaml parser, e.g. PyYAML
raw = yaml.safe_load(self.fspath.open())
for name, spec in sorted(raw.items()):
2010-11-06 06:37:25 +08:00
yield YamlItem(name, self, 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",
" spec failed: %r: %r" % excinfo.value.args[1:3],
" no further details known at this point.",
]
)
def reportinfo(self):
return self.fspath, 0, "usecase: %s" % 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. """