fixture.py revision 12882:dd87d7f2f3e5
1# Copyright (c) 2017 Mark D. Hill and David A. Wood
2# All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met: redistributions of source code must retain the above copyright
7# notice, this list of conditions and the following disclaimer;
8# redistributions in binary form must reproduce the above copyright
9# notice, this list of conditions and the following disclaimer in the
10# documentation and/or other materials provided with the distribution;
11# neither the name of the copyright holders nor the names of its
12# contributors may be used to endorse or promote products derived from
13# this software without specific prior written permission.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27# Authors: Sean Wilson
28
29import copy
30import traceback
31
32import helper
33import log
34
35global_fixtures = []
36
37class SkipException(Exception):
38    def __init__(self, fixture, testitem):
39        self.fixture = fixture
40        self.testitem = testitem
41
42        self.msg = 'Fixture "%s" raised SkipException for "%s".' % (
43               fixture.name, testitem.name
44        )
45        super(SkipException, self).__init__(self.msg)
46
47
48class Fixture(object):
49    '''
50    Base Class for a test Fixture.
51
52    Fixtures are items which possibly require setup and/or tearing down after
53    a TestCase, TestSuite, or the Library has completed.
54
55    Fixtures are the prefered method of carrying incremental results or
56    variables between TestCases in TestSuites. (Rather than using globals.)
57    Using fixtures rather than globals ensures that state will be maintained
58    when executing tests in parallel.
59
60    .. note:: In order for Fixtures to be enumerated by the test system this
61        class' :code:`__new__` method must be called.
62    '''
63    collector = helper.InstanceCollector()
64
65    def __new__(klass, *args, **kwargs):
66        obj = super(Fixture, klass).__new__(klass, *args, **kwargs)
67        Fixture.collector.collect(obj)
68        return obj
69
70    def __init__(self, name=None, **kwargs):
71        if name is None:
72            name = self.__class__.__name__
73        self.name = name
74
75    def skip(self, testitem):
76        raise SkipException(self.name, testitem.metadata)
77
78    def schedule_finalized(self, schedule):
79        '''
80        This method is called once the schedule of for tests is known.
81        To enable tests to use the same fixture defintion for each execution
82        fixtures must return a copy of themselves in this method.
83
84        :returns: a copy of this fixture which will be setup/torndown
85          when the test item this object is tied to is about to execute.
86        '''
87        return self.copy()
88
89    def init(self, *args, **kwargs):
90        pass
91
92    def setup(self, testitem):
93        pass
94
95    def teardown(self, testitem):
96        pass
97
98    def copy(self):
99        return copy.deepcopy(self)
100
101
102def globalfixture(fixture):
103    '''
104    Store the given fixture as a global fixture. Its setup() method
105    will be called before the first test is executed.
106    '''
107    global_fixtures.append(fixture)
108    return fixture
109