summaryrefslogtreecommitdiff
path: root/buildscripts/resmokelib/testing/hooks/interface.py
blob: 75f222bbdd1e8d83a0cf9044531c5e4dd7e09db5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Interface for customizing the behavior of a test fixture.
"""

from __future__ import absolute_import

from ...logging import loggers
from ...utils import registry


_HOOKS = {}


def make_hook(class_name, *args, **kwargs):
    """
    Factory function for creating Hook instances.
    """

    if class_name not in _HOOKS:
        raise ValueError("Unknown hook class '%s'" % class_name)

    return _HOOKS[class_name](*args, **kwargs)


class Hook(object):
    """
    The common interface all Hooks will inherit from.
    """

    __metaclass__ = registry.make_registry_metaclass(_HOOKS)

    REGISTERED_NAME = registry.LEAVE_UNREGISTERED

    @staticmethod
    def start_dynamic_test(hook_test_case, test_report):
        """
        If a Hook wants to add a test case that will show up
        in the test report, it should use this method to add it to the
        report, since we will need to count it as a dynamic test to get
        the stats in the summary information right.
        """
        test_report.startTest(hook_test_case, dynamic=True)

    def __init__(self, hook_logger, fixture, description):
        """
        Initializes the Hook with the specified fixture.
        """

        if not isinstance(hook_logger, loggers.HookLogger):
            raise TypeError("logger must be a HookLogger instance")

        self.logger = hook_logger
        self.fixture = fixture
        self.description = description
        self.hook_test_case = None

    def make_dynamic_test(self, test_case_class, *args, **kwargs):
        """
        Returns an instance of 'test_case_class' configured to use the
        appropriate logger.
        """
        return test_case_class(self.logger.test_case_logger, *args, **kwargs)

    def before_suite(self, test_report):
        """
        The test runner calls this exactly once before they start
        running the suite.
        """
        pass

    def after_suite(self, test_report):
        """
        The test runner calls this exactly once after all tests have
        finished executing. Be sure to reset the behavior back to its
        original state so that it can be run again.
        """
        pass

    def before_test(self, test, test_report):
        """
        Each test will call this before it executes.
        """
        pass

    def after_test(self, test, test_report):
        """
        Each test will call this after it executes.
        """
        pass