#!/usr/bin/python # Copyright (c) 2005-2006 by Mark T. Holder, Florida State University. (see end of file) '''Base class and some standard imports to make it simple to write a client that uses the CIPRES library to test a new service. The base class inherits from unittest.TestCase and is triggered in the same mechanism as unittest (call main and all of the methods that start with "test" in classes that subclass CipresTestingClient will be evaluated). Provides a static method ("getTestingRegistry") that refers to the CIPRES registry. Intended to be imported using * such as: from PIPRes.testing import * ''' import CORBA from PIPRes.cipres_types import * from PIPRes.basic import * from PIPRes.util.io import expandPath, getCipresTestFile import PIPRes.util.io from PIPRes.util.cipres_registry_from_xml import composeRegistryQuery from PIPRes.wrap.idl import CorbaError import unittest _LOG = cipresGetLogger('test.service_impl.paup_wrap_tree_infer') def main(*args, **kwargs): try: unittest.main() except CorbaError: logException(_LOG) _LOG.warn('Test skipped: CORBA error.') def silentAssertRaises(test_obj, excep_type, *arg_list, **kwargs): prev = PIPRes.util.io.SUPPRESS_EXCEPTION_LOGGING try: PIPRes.util.io.SUPPRESS_EXCEPTION_LOGGING = True test_obj.assertRaises(excep_type, *arg_list, **kwargs) finally: PIPRes.util.io.SUPPRESS_EXCEPTION_LOGGING = prev class CipresTestingClient(unittest.TestCase, object): _registry = None def getTestingRegistry(): if CipresTestingClient._registry is None: CipresTestingClient._registry = getCipresRegistry() return CipresTestingClient._registry getTestingRegistry = staticmethod(getTestingRegistry) def getRegistry(self): return CipresTestingClient.getTestingRegistry() # In a subclass serviceRegistryEntry should be set to a registry entry # (it is easiest to do this by calling composeRegistryQuery). Only # needed if getTestedService is called. serviceRegistryEntry = None # In a subclass serviceType should be the class (such as CipresIDL_api1.MatrixAlign # which is being tested). Only needed if getTestedService is called. serviceType = None # service factory can be set to a callable that takes a registry as the only # arg. If not None, serviceFactory will be used instead of asking the # registry for a new instance of the service serviceFactory = None def getTestedService(self): '''Uses the registry and self's serviceRegistryEntry and serviceType to create a new instance to be tested.''' reg = self.getRegistry() if self.serviceFactory is not None: return self.serviceFactory(reg) if self.serviceRegistryEntry is None: _LOG.debug('To call use self.testedService, serviceRegistryEntry cannot be None') return None if self.serviceType is None: LOG.debug('To call self.testedService, serviceType cannot be None') return None return reg.getServiceOrThrow(self.serviceType, registryEntry=self.serviceRegistryEntry) def newServiceSetUp(self): '''Can be used as a setUp overload if you want to use getTestedService to get a "fresh" instance of the service for every test.''' self.testedService= None self.testedService= self.getTestedService() def newServiceTearDown(self): if self.testedService is not None: self.testedService.remove() self.testedService= None def testFromCipresTests(self): c = self.__class__ t = getattr(c, "_cipresTests", None) if t is None: addCipresTests(c) t = getattr(c, "_cipresTests") assert t is not None self.tearDown() try: for p in t: name, func = p[0], p[1] try: self.setUp() self.newServiceSetUp() except CipresIDL_api1.NoRegistryMatchException, x: _LOG.warn('Test skipped: ' + x.msg) except : logException(_LOG) _LOG.warn('Test skipped: Registry error.') else: try: _LOG.debug("Running %s" % name) func(self) finally: self.newServiceTearDown() self.tearDown() finally: self.setUp() skipped = getattr(c, "_skippedCipresTests", None) if skipped: _LOG.warn("Skipped tests:\n%s\n" % "\n".join(skipped)) def addCipresTests(c): '''Converts cipresTest methods to test methods. These tests will also issue warnings for Registry errors instead of failing tests. Use of "cipresTest..." methods requires you to set serviceRegistryEntry and serviceType as your subclass (as statics or in __init__ ). ''' c._cipresTests = [] c._skippedCipresTests = [] for k, v in c.__dict__.iteritems(): if callable(v): if k.lower().startswith('ciprestest'): t = (k, v) c._cipresTests.append(t) elif k.lower().startswith("skipciprestest"): c._skippedCipresTests.append(k) # This file is part of the PIPRes library # # The PIPRes library is free software; you can redistribute it # and/or modify it under the terms of the GNU Lesser General # Public License as published by the Free Software Foundation; # either version 2.1 of the License, or (at your option) any later # version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free # Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, # MA 02111-1307, USA