Skip to content

Commit

Permalink
Add hook which is called before every test
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelboulton committed Aug 21, 2019
1 parent 7c014d0 commit 04f0972
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
4 changes: 2 additions & 2 deletions tavern/testutils/pytesthook/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .hooks import pytest_collect_file, pytest_addoption
from .hooks import pytest_collect_file, pytest_addoption, pytest_addhooks
from .util import add_parser_options

__all__ = ["pytest_addoption", "pytest_collect_file", "add_parser_options"]
__all__ = ["pytest_addoption", "pytest_collect_file", "pytest_addhooks", "add_parser_options"]
7 changes: 7 additions & 0 deletions tavern/testutils/pytesthook/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ def pytest_collect_file(parent, path):
return YamlFile(path, parent)

return None


def pytest_addhooks(pluginmanager):
"""Add our custom tavern hooks"""
from . import newhooks

pluginmanager.add_hookspecs(newhooks)
13 changes: 11 additions & 2 deletions tavern/testutils/pytesthook/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,27 @@ def runtest(self):

load_plugins(self.global_cfg)

self.global_cfg["tavern_internal"] = {"pytest_hook_caller": self.config.hook}

# INTERNAL
# NOTE - now that we can 'mark' tests, we could use pytest.mark.xfail
# instead. This doesn't differentiate between an error in verification
# and an error when running the test though.
xfail = self.spec.get("_xfail", False)

try:
verify_tests(self.spec)

fixture_values = self._load_fixture_values()
self.global_cfg["variables"].update(fixture_values)

self.global_cfg["tavern_internal"][
"pytest_hook_caller"
].pytest_tavern_before_every_test_run(
test_dict=self.spec,
variables=self.global_cfg["variables"]
)

verify_tests(self.spec)

run_test(self.path, self.spec, self.global_cfg)
except exceptions.BadSchemaError:
if xfail == "verify":
Expand Down
19 changes: 19 additions & 0 deletions tavern/testutils/pytesthook/newhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# pylint: disable=unused-argument


def pytest_tavern_before_every_test_run(test_dict, variables):
"""Called:
- directly after fixtures are loaded for a test
- directly before verifying the schema of the file
- Before formatting is done on values
- After fixtures have been resolved
- After global configuration has been loaded
- After plugins have been loaded
Modify the test in-place if you want to do something to it.
Args:
test_dict (dict): Test to run
variables (dict): Available variables
"""
2 changes: 2 additions & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from mock import Mock
import pytest


Expand All @@ -13,6 +14,7 @@ def fix_example_includes():
},
"backends": {"mqtt": "paho-mqtt", "http": "requests"},
"strict": True,
"tavern_internal": {"pytest_hook_caller": Mock()},
}

return includes.copy()

0 comments on commit 04f0972

Please sign in to comment.