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
|
|
|
|
2012-11-01 00:00:55 +08:00
|
|
|
def pytest_collect_file(parent, path):
|
2019-07-03 14:49:06 +08:00
|
|
|
if path.ext == ".yaml" and path.basename.startswith("test"):
|
2010-10-27 20:52:28 +08:00
|
|
|
return YamlFile(path, parent)
|
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):
|
2018-05-23 22:48:46 +08:00
|
|
|
import yaml # we need a yaml parser, e.g. PyYAML
|
|
|
|
|
2013-05-16 15:59:48 +08:00
|
|
|
raw = yaml.safe_load(self.fspath.open())
|
2016-08-02 06:37:52 +08:00
|
|
|
for name, spec in sorted(raw.items()):
|
2010-11-06 06:37:25 +08:00
|
|
|
yield YamlItem(name, self, 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()):
|
2010-10-27 20:52:28 +08:00
|
|
|
# some custom test execution (dumb example follows)
|
|
|
|
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):
|
|
|
|
""" 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.",
|
|
|
|
]
|
|
|
|
)
|
2010-10-27 20:52:28 +08:00
|
|
|
|
|
|
|
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):
|
2010-10-27 20:52:28 +08:00
|
|
|
""" custom exception for error reporting. """
|