Работа с тестами не на python
¶
Простой пример поиска тестов в файлах «yaml»¶
Вот пример файла conftest.py
(полученного из плагина pytest-yamlwsgi).
Этот conftest.py
собирает файлы вида test*.yaml
и выполняет их содержимое в виде тестов:
# content of conftest.py
import pytest
def pytest_collect_file(parent, path):
if path.ext == ".yaml" and path.basename.startswith("test"):
return YamlFile.from_parent(parent, fspath=path)
class YamlFile(pytest.File):
def collect(self):
import yaml # we need a yaml parser, e.g. PyYAML
raw = yaml.safe_load(self.fspath.open())
for name, spec in sorted(raw.items()):
yield YamlItem.from_parent(self, name=name, spec=spec)
class YamlItem(pytest.Item):
def __init__(self, name, parent, spec):
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:
raise YamlException(self, name, value)
def repr_failure(self, excinfo):
""" called when self.runtest() raises an exception. """
if isinstance(excinfo.value, YamlException):
return "\n".join(
[
"usecase execution failed",
" spec failed: {1!r}: {2!r}".format(*excinfo.value.args),
" no further details known at this point.",
]
)
def reportinfo(self):
return self.fspath, 0, "usecase: {}".format(self.name)
class YamlException(Exception):
""" custom exception for error reporting. """
Можно создать простой тестовый файл:
# test_simple.yaml
ok:
sub1: sub1
hello:
world: world
some: other
И если вы установите PyYAML или совместимый YAML-парсер, то сможете запустить тесты:
nonpython $ pytest test_simple.yaml
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR/nonpython
collected 2 items
test_simple.yaml F. [100%]
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
======================= 1 failed, 1 passed in 0.12s ========================
У нас получился один прошедший тест на проверку sub1: sub1
и один упавший.
Очевидно, вы можете захотеть реализовать в conftest.py
более интересную интерпретацию значений. Так можно легко
написать свой собственный «язык тестирования».
Примечание
repr_failure(excinfo)
вызывается для представления упавших тестов.
Если вы сами будете генерировать узлы, то сможете возвращать
любое строковое представление ошибки по вашему выбору. В отчете
оно будет выделено красным.
reportinfo()
используется для представления расположения теста, а также
в режиме verbose
:
nonpython $ pytest -v
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y -- $PYTHON_PREFIX/bin/python
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR/nonpython
collecting ... collected 2 items
test_simple.yaml::hello FAILED [ 50%]
test_simple.yaml::ok PASSED [100%]
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
======================= 1 failed, 1 passed in 0.12s ========================
При разработке собственного поиска и выполнения тестов интересно будет взглянуть на дерево собранных тестов:
nonpython $ pytest --collect-only
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-5.x.y, py-1.x.y, pluggy-0.x.y
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR/nonpython
collected 2 items
<Package $REGENDOC_TMPDIR/nonpython>
<YamlFile test_simple.yaml>
<YamlItem hello>
<YamlItem ok>
========================== no tests ran in 0.12s ===========================