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 35class SkipException(Exception): 36 def __init__(self, fixture, testitem): 37 self.fixture = fixture 38 self.testitem = testitem 39 40 self.msg = 'Fixture "%s" raised SkipException for "%s".' % ( 41 fixture.name, testitem.name 42 ) 43 super(SkipException, self).__init__(self.msg) 44 45 46class Fixture(object): 47 ''' 48 Base Class for a test Fixture. 49 50 Fixtures are items which possibly require setup and/or tearing down after 51 a TestCase, TestSuite, or the Library has completed. 52 53 Fixtures are the prefered method of carrying incremental results or 54 variables between TestCases in TestSuites. (Rather than using globals.) 55 Using fixtures rather than globals ensures that state will be maintained 56 when executing tests in parallel. 57 58 .. note:: In order for Fixtures to be enumerated by the test system this 59 class' :code:`__new__` method must be called. 60 ''' 61 collector = helper.InstanceCollector() 62 63 def __new__(klass, *args, **kwargs): 64 obj = super(Fixture, klass).__new__(klass, *args, **kwargs) 65 Fixture.collector.collect(obj) 66 return obj 67 68 def __init__(self, name=None, **kwargs): 69 if name is None: 70 name = self.__class__.__name__ 71 self.name = name 72 self._is_global = False 73 74 def skip(self, testitem): 75 raise SkipException(self.name, testitem.metadata) 76 77 def init(self, *args, **kwargs): 78 pass 79 80 def setup(self, testitem): 81 pass 82 83 def teardown(self, testitem): 84 pass 85 86 def skip_cleanup(self): 87 ''' 88 If this method is called, then we should make sure that nothing is 89 done when the teardown() function is called. 90 ''' 91 pass 92 93 def set_global(self): 94 self._is_global = True 95 96 def is_global(self): 97 return self._is_global 98