From 9ee421ec6db5a49b63a84a20a08889cc8846fe37 Mon Sep 17 00:00:00 2001 From: Michael Boulton Date: Sat, 17 Aug 2019 15:41:02 +0100 Subject: [PATCH] Add hook which is called before every test --- tavern/testutils/pytesthook/__init__.py | 4 ++-- tavern/testutils/pytesthook/hooks.py | 7 +++++++ tavern/testutils/pytesthook/item.py | 13 +++++++++++-- tavern/testutils/pytesthook/newhooks.py | 19 +++++++++++++++++++ tests/unit/conftest.py | 2 ++ 5 files changed, 41 insertions(+), 4 deletions(-) create mode 100644 tavern/testutils/pytesthook/newhooks.py diff --git a/tavern/testutils/pytesthook/__init__.py b/tavern/testutils/pytesthook/__init__.py index 5028b0b5e..cb66a34d5 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 4d991dd1f..d45f477c1 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 0c055f4d2..89e593993 100644 --- a/tavern/testutils/pytesthook/item.py +++ b/tavern/testutils/pytesthook/item.py @@ -137,6 +137,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 @@ -144,11 +146,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 000000000..762724bb6 --- /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 3a6a1a50e..6c0d7bd84 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()