test.py revision 13791
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 functools 3012882Sspwilson2@wisc.edu 3112882Sspwilson2@wisc.eduimport helper 3212882Sspwilson2@wisc.eduimport runner as runner_mod 3312882Sspwilson2@wisc.edu 3413791Sjason@lowepower.comclass TestingException(Exception): 3513791Sjason@lowepower.com '''Common ancestor for manual Testing Exceptions.''' 3613791Sjason@lowepower.comclass TestFailException(TestingException): 3713791Sjason@lowepower.com '''Signals that a test has failed.''' 3813791Sjason@lowepower.comclass TestSkipException(TestingException): 3913791Sjason@lowepower.com '''Signals that a test has been skipped.''' 4013791Sjason@lowepower.com 4113791Sjason@lowepower.comdef fail(message): 4213791Sjason@lowepower.com '''Cause the current test to fail with the given message.''' 4313791Sjason@lowepower.com raise TestFailException(message) 4413791Sjason@lowepower.com 4513791Sjason@lowepower.comdef skip(message): 4613791Sjason@lowepower.com '''Cause the current test to skip with the given message.''' 4713791Sjason@lowepower.com raise TestSkipException(message) 4813791Sjason@lowepower.com 4912882Sspwilson2@wisc.educlass TestCase(object): 5012882Sspwilson2@wisc.edu ''' 5112882Sspwilson2@wisc.edu Base class for all tests. 5212882Sspwilson2@wisc.edu 5312882Sspwilson2@wisc.edu ..note:: 5412882Sspwilson2@wisc.edu The :func:`__new__` method enables collection of test cases, it must 5512882Sspwilson2@wisc.edu be called in order for test cases to be collected. 5612882Sspwilson2@wisc.edu ''' 5712882Sspwilson2@wisc.edu fixtures = [] 5812882Sspwilson2@wisc.edu 5912882Sspwilson2@wisc.edu # TODO, remove explicit dependency. Use the loader to set the 6012882Sspwilson2@wisc.edu # default runner 6112882Sspwilson2@wisc.edu runner = runner_mod.TestRunner 6212882Sspwilson2@wisc.edu collector = helper.InstanceCollector() 6312882Sspwilson2@wisc.edu 6412882Sspwilson2@wisc.edu def __new__(cls, *args, **kwargs): 6512882Sspwilson2@wisc.edu obj = super(TestCase, cls).__new__(cls, *args, **kwargs) 6612882Sspwilson2@wisc.edu TestCase.collector.collect(obj) 6712882Sspwilson2@wisc.edu return obj 6812882Sspwilson2@wisc.edu 6912882Sspwilson2@wisc.edu def __init__(self, name=None, fixtures=tuple(), **kwargs): 7012882Sspwilson2@wisc.edu self.fixtures = self.fixtures + list(fixtures) 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.educlass TestFunction(TestCase): 7612882Sspwilson2@wisc.edu ''' 7712882Sspwilson2@wisc.edu TestCase implementation which uses a callable object as a test. 7812882Sspwilson2@wisc.edu ''' 7912882Sspwilson2@wisc.edu def __init__(self, function, name=None, **kwargs): 8012882Sspwilson2@wisc.edu self.test_function = function 8112882Sspwilson2@wisc.edu if name is None: 8212882Sspwilson2@wisc.edu name = function.__name__ 8312882Sspwilson2@wisc.edu TestCase.__init__(self, name=name, **kwargs) 8412882Sspwilson2@wisc.edu 8512882Sspwilson2@wisc.edu def test(self, *args, **kwargs): 8612882Sspwilson2@wisc.edu self.test_function(*args, **kwargs) 8712882Sspwilson2@wisc.edu 8812882Sspwilson2@wisc.edu# TODO Change the decorator to make this easier to create copy tests. 8912882Sspwilson2@wisc.edu# Good way to do so might be return by reference. 9012882Sspwilson2@wisc.edudef testfunction(function=None, name=None, fixtures=tuple()): 9112882Sspwilson2@wisc.edu ''' 9212882Sspwilson2@wisc.edu A decorator used to wrap a function as a TestFunction. 9312882Sspwilson2@wisc.edu ''' 9412882Sspwilson2@wisc.edu def testfunctiondecorator(function): 9512882Sspwilson2@wisc.edu '''Decorator used to mark a function as a test case.''' 9612882Sspwilson2@wisc.edu kwargs = {} 9712882Sspwilson2@wisc.edu if name is not None: 9812882Sspwilson2@wisc.edu kwargs['name'] = name 9912882Sspwilson2@wisc.edu if fixtures is not None: 10012882Sspwilson2@wisc.edu kwargs['fixtures'] = fixtures 10112882Sspwilson2@wisc.edu TestFunction(function, **kwargs) 10212882Sspwilson2@wisc.edu return function 10312882Sspwilson2@wisc.edu if function is not None: 10412882Sspwilson2@wisc.edu return testfunctiondecorator(function) 10512882Sspwilson2@wisc.edu else: 10613791Sjason@lowepower.com return testfunctiondecorator 107