diff --git a/tavern/testutils/pytesthook/__init__.py b/tavern/testutils/pytesthook/__init__.py index 5028b0b5..cb66a34d 100644 --- a/tavern/testutils/pytesthook/__init__.py +++ b/tavern/testutils/pytesthook/__init__.py @@ -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"] diff --git a/tavern/testutils/pytesthook/hooks.py b/tavern/testutils/pytesthook/hooks.py index 4d991dd1..d45f477c 100644 --- a/tavern/testutils/pytesthook/hooks.py +++ b/tavern/testutils/pytesthook/hooks.py @@ -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) diff --git a/tavern/testutils/pytesthook/item.py b/tavern/testutils/pytesthook/item.py index b6f6d376..3676f6d0 100644 --- a/tavern/testutils/pytesthook/item.py +++ b/tavern/testutils/pytesthook/item.py @@ -139,6 +139,8 @@ 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 @@ -146,11 +148,18 @@ def runtest(self): 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": diff --git a/tavern/testutils/pytesthook/newhooks.py b/tavern/testutils/pytesthook/newhooks.py new file mode 100644 index 00000000..762724bb --- /dev/null +++ b/tavern/testutils/pytesthook/newhooks.py @@ -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 + """ diff --git a/tests/unit/conftest.py b/tests/unit/conftest.py index 3a6a1a50..6c0d7bd8 100644 --- a/tests/unit/conftest.py +++ b/tests/unit/conftest.py @@ -1,3 +1,4 @@ +from mock import Mock import pytest @@ -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()