conftest.py (Ali Afshnars の特殊用途の pytest-yamlwsgi プラグインから引用) のサンプルを紹介します。この conftest.py は test*.yml ファイルを探してきて、yaml フォーマットのコンテンツをカスタムテストとして実行します:
# content of conftest.py
import pytest
def pytest_collect_file(path, parent):
if path.ext == ".yml" and path.basename.startswith("test"):
return YamlFile(path, parent)
class YamlFile(pytest.File):
def collect(self):
import yaml # we need a yaml parser, e.g. PyYAML
raw = yaml.load(self.fspath.open())
for name, spec in raw.items():
yield YamlItem(name, self, spec)
class YamlItem(pytest.Item):
def __init__(self, name, parent, spec):
super(YamlItem, self).__init__(name, parent)
self.spec = spec
def runtest(self):
for name, value in 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: %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
class YamlException(Exception):
""" custom exception for error reporting. """
簡単なサンプルファイルを作成します:
# test_simple.yml
ok:
sub1: sub1
hello:
world: world
some: other
PyYAML か、互換性のある YAML パーサーをインストール済みなら、そのテスト仕様を実行できます:
nonpython $ py.test test_simple.yml
=========================== test session starts ============================
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
collecting ... collected 2 items
test_simple.yml .F
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
==================== 1 failed, 1 passed in 0.06 seconds ====================
sub1: sub1 は成功してドットを1つ表示し、もう1つは失敗します。上述した conftest.py は言うまでもなく単純なので、もっとおもしろい yaml 値を解釈するサンプルを実装したくなるでしょう。このように独自のドメイン固有テスト言語を簡単に記述できます。
注釈
repr_failure(excinfo) はテストの失敗を表現するために呼ばれます。カスタムコレクションのノードを作成する場合、好きなエラーを表現する文字列を返せます。それは (赤い) 文字列で表示されます。
reportinfo() はテストの位置を表現したり、 verbose モードではレポート時にも使われます:
nonpython $ py.test -v
=========================== test session starts ============================
platform linux2 -- Python 2.7.1 -- pytest-2.2.4 -- /home/hpk/venv/0/bin/python
collecting ... collected 2 items
test_simple.yml:1: usecase: ok PASSED
test_simple.yml:1: usecase: hello FAILED
================================= FAILURES =================================
______________________________ usecase: hello ______________________________
usecase execution failed
spec failed: 'some': 'other'
no further details known at this point.
==================== 1 failed, 1 passed in 0.06 seconds ====================
カスタムテストコレクションや実行処理の開発中、そのコレクションツリーをちょっと見るのもおもしろいです:
nonpython $ py.test --collect-only
=========================== test session starts ============================
platform linux2 -- Python 2.7.1 -- pytest-2.2.4
collecting ... collected 2 items
<YamlFile 'test_simple.yml'>
<YamlItem 'ok'>
<YamlItem 'hello'>
============================= in 0.07 seconds =============================