fixture.py revision 13790
112882Sspwilson2@wisc.edu# Copyright (c) 2017 Mark D. Hill and David A. Wood
212882Sspwilson2@wisc.edu# All rights reserved.
312882Sspwilson2@wisc.edu#
412882Sspwilson2@wisc.edu# Redistribution and use in source and binary forms, with or without
512882Sspwilson2@wisc.edu# modification, are permitted provided that the following conditions are
612882Sspwilson2@wisc.edu# met: redistributions of source code must retain the above copyright
712882Sspwilson2@wisc.edu# notice, this list of conditions and the following disclaimer;
812882Sspwilson2@wisc.edu# redistributions in binary form must reproduce the above copyright
912882Sspwilson2@wisc.edu# notice, this list of conditions and the following disclaimer in the
1012882Sspwilson2@wisc.edu# documentation and/or other materials provided with the distribution;
1112882Sspwilson2@wisc.edu# neither the name of the copyright holders nor the names of its
1212882Sspwilson2@wisc.edu# contributors may be used to endorse or promote products derived from
1312882Sspwilson2@wisc.edu# this software without specific prior written permission.
1412882Sspwilson2@wisc.edu#
1512882Sspwilson2@wisc.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1612882Sspwilson2@wisc.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
1712882Sspwilson2@wisc.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
1812882Sspwilson2@wisc.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
1912882Sspwilson2@wisc.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2012882Sspwilson2@wisc.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2112882Sspwilson2@wisc.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2212882Sspwilson2@wisc.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2312882Sspwilson2@wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2412882Sspwilson2@wisc.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2512882Sspwilson2@wisc.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2612882Sspwilson2@wisc.edu#
2712882Sspwilson2@wisc.edu# Authors: Sean Wilson
2812882Sspwilson2@wisc.edu
2912882Sspwilson2@wisc.eduimport copy
3012882Sspwilson2@wisc.eduimport traceback
3112882Sspwilson2@wisc.edu
3212882Sspwilson2@wisc.eduimport helper
3312882Sspwilson2@wisc.eduimport log
3412882Sspwilson2@wisc.edu
3512882Sspwilson2@wisc.eduglobal_fixtures = []
3612882Sspwilson2@wisc.edu
3712882Sspwilson2@wisc.educlass SkipException(Exception):
3812882Sspwilson2@wisc.edu    def __init__(self, fixture, testitem):
3912882Sspwilson2@wisc.edu        self.fixture = fixture
4012882Sspwilson2@wisc.edu        self.testitem = testitem
4112882Sspwilson2@wisc.edu
4212882Sspwilson2@wisc.edu        self.msg = 'Fixture "%s" raised SkipException for "%s".' % (
4312882Sspwilson2@wisc.edu               fixture.name, testitem.name
4412882Sspwilson2@wisc.edu        )
4512882Sspwilson2@wisc.edu        super(SkipException, self).__init__(self.msg)
4612882Sspwilson2@wisc.edu
4712882Sspwilson2@wisc.edu
4812882Sspwilson2@wisc.educlass Fixture(object):
4912882Sspwilson2@wisc.edu    '''
5012882Sspwilson2@wisc.edu    Base Class for a test Fixture.
5112882Sspwilson2@wisc.edu
5212882Sspwilson2@wisc.edu    Fixtures are items which possibly require setup and/or tearing down after
5312882Sspwilson2@wisc.edu    a TestCase, TestSuite, or the Library has completed.
5412882Sspwilson2@wisc.edu
5512882Sspwilson2@wisc.edu    Fixtures are the prefered method of carrying incremental results or
5612882Sspwilson2@wisc.edu    variables between TestCases in TestSuites. (Rather than using globals.)
5712882Sspwilson2@wisc.edu    Using fixtures rather than globals ensures that state will be maintained
5812882Sspwilson2@wisc.edu    when executing tests in parallel.
5912882Sspwilson2@wisc.edu
6012882Sspwilson2@wisc.edu    .. note:: In order for Fixtures to be enumerated by the test system this
6112882Sspwilson2@wisc.edu        class' :code:`__new__` method must be called.
6212882Sspwilson2@wisc.edu    '''
6312882Sspwilson2@wisc.edu    collector = helper.InstanceCollector()
6412882Sspwilson2@wisc.edu
6512882Sspwilson2@wisc.edu    def __new__(klass, *args, **kwargs):
6612882Sspwilson2@wisc.edu        obj = super(Fixture, klass).__new__(klass, *args, **kwargs)
6712882Sspwilson2@wisc.edu        Fixture.collector.collect(obj)
6812882Sspwilson2@wisc.edu        return obj
6912882Sspwilson2@wisc.edu
7012882Sspwilson2@wisc.edu    def __init__(self, name=None, **kwargs):
7112882Sspwilson2@wisc.edu        if name is None:
7212882Sspwilson2@wisc.edu            name = self.__class__.__name__
7312882Sspwilson2@wisc.edu        self.name = name
7412882Sspwilson2@wisc.edu
7512882Sspwilson2@wisc.edu    def skip(self, testitem):
7612882Sspwilson2@wisc.edu        raise SkipException(self.name, testitem.metadata)
7712882Sspwilson2@wisc.edu
7812882Sspwilson2@wisc.edu    def schedule_finalized(self, schedule):
7912882Sspwilson2@wisc.edu        '''
8012882Sspwilson2@wisc.edu        This method is called once the schedule of for tests is known.
8112882Sspwilson2@wisc.edu        To enable tests to use the same fixture defintion for each execution
8212882Sspwilson2@wisc.edu        fixtures must return a copy of themselves in this method.
8312882Sspwilson2@wisc.edu
8412882Sspwilson2@wisc.edu        :returns: a copy of this fixture which will be setup/torndown
8512882Sspwilson2@wisc.edu          when the test item this object is tied to is about to execute.
8612882Sspwilson2@wisc.edu        '''
8712882Sspwilson2@wisc.edu        return self.copy()
8812882Sspwilson2@wisc.edu
8912882Sspwilson2@wisc.edu    def init(self, *args, **kwargs):
9012882Sspwilson2@wisc.edu        pass
9112882Sspwilson2@wisc.edu
9212882Sspwilson2@wisc.edu    def setup(self, testitem):
9312882Sspwilson2@wisc.edu        pass
9412882Sspwilson2@wisc.edu
9512882Sspwilson2@wisc.edu    def teardown(self, testitem):
9612882Sspwilson2@wisc.edu        pass
9712882Sspwilson2@wisc.edu
9812882Sspwilson2@wisc.edu    def copy(self):
9912882Sspwilson2@wisc.edu        return copy.deepcopy(self)
10012882Sspwilson2@wisc.edu
10113790Sjason@lowepower.com    def skip_cleanup(self):
10213790Sjason@lowepower.com        '''
10313790Sjason@lowepower.com        If this method is called, then we should make sure that nothing is
10413790Sjason@lowepower.com        done when the teardown() function is called.
10513790Sjason@lowepower.com        '''
10613790Sjason@lowepower.com        pass
10713790Sjason@lowepower.com
10812882Sspwilson2@wisc.edu
10912882Sspwilson2@wisc.edudef globalfixture(fixture):
11012882Sspwilson2@wisc.edu    '''
11112882Sspwilson2@wisc.edu    Store the given fixture as a global fixture. Its setup() method
11212882Sspwilson2@wisc.edu    will be called before the first test is executed.
11312882Sspwilson2@wisc.edu    '''
11412882Sspwilson2@wisc.edu    global_fixtures.append(fixture)
11512882Sspwilson2@wisc.edu    return fixture
116