<s> """<STR_LIT>""" <EOL> import unittest <EOL> import sys <EOL> from nose2 import events , util <EOL> from nose2 . compat import unittest as ut2 <EOL> __unittest = True <EOL> class TestClassLoader ( events . Plugin ) : <EOL> """<STR_LIT>""" <EOL> alwaysOn = True <EOL> configSection = '<STR_LIT>' <EOL> def registerInSubprocess ( self , event ) : <EOL> event . pluginClasses . append ( self . __class__ ) <EOL> def register ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( TestClassLoader , self ) . register ( ) <EOL> self . addMethods ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def loadTestsFromModule ( self , event ) : <EOL> """<STR_LIT>""" <EOL> module = event . module <EOL> for name in dir ( module ) : <EOL> obj = getattr ( module , name ) <EOL> if ( isinstance ( obj , type ) and <EOL> not issubclass ( obj , unittest . TestCase ) and <EOL> not issubclass ( obj , unittest . TestSuite ) and <EOL> name . lower ( ) . startswith ( self . session . testMethodPrefix ) ) : <EOL> event . extraTests . append ( <EOL> self . _loadTestsFromTestClass ( event , obj ) ) <EOL> def loadTestsFromName ( self , event ) : <EOL> """<STR_LIT>""" <EOL> name = event . name <EOL> module = event . module <EOL> try : <EOL> result = util . test_from_name ( name , module ) <EOL> except ( AttributeError , ImportError ) : <EOL> event . handled = True <EOL> return event . loader . failedLoadTests ( name , sys . exc_info ( ) ) <EOL> if result is None : <EOL> return <EOL> parent , obj , name , index = result <EOL> if isinstance ( obj , type ) and not issubclass ( obj , unittest . TestCase ) : <EOL> event . extraTests . append ( self . _loadTestsFromTestClass ( event , obj ) ) <EOL> elif ( isinstance ( parent , type ) and <EOL> not issubclass ( parent , unittest . TestCase ) and <EOL> not util . isgenerator ( obj ) and <EOL> not hasattr ( obj , '<STR_LIT>' ) ) : <EOL> event . extraTests . append ( <EOL> util . transplant_class ( <EOL> MethodTestCase ( parent ) , parent . __module__ ) ( obj . __name__ ) ) <EOL> def _loadTestsFromTestClass ( self , event , cls ) : <EOL> evt = LoadFromTestClassEvent ( event . loader , cls ) <EOL> result = self . session . hooks . loadTestsFromTestClass ( evt ) <EOL> if evt . handled : <EOL> loaded_suite = result or event . loader . suiteClass ( ) <EOL> else : <EOL> names = self . _getTestMethodNames ( event , cls ) <EOL> try : <EOL> loaded_suite = event . loader . suiteClass ( <EOL> [ util . transplant_class ( <EOL> MethodTestCase ( cls ) , cls . __module__ ) ( name ) <EOL> for name in names ] ) <EOL> except : <EOL> return event . loader . suiteClass ( <EOL> event . loader . failedLoadTests ( cls . __name__ , sys . exc_info ( ) ) ) <EOL> if evt . extraTests : <EOL> loaded_suite . addTests ( evt . extraTests ) <EOL> return loaded_suite <EOL> def _getTestMethodNames ( self , event , cls ) : <EOL> excluded = set ( ) <EOL> def isTestMethod ( attrname , cls = cls , excluded = excluded ) : <EOL> prefix = self . session . testMethodPrefix <EOL> return ( <EOL> attrname . startswith ( prefix ) and <EOL> hasattr ( getattr ( cls , attrname ) , '<STR_LIT>' ) and <EOL> attrname not in excluded <EOL> ) <EOL> evt = GetTestMethodNamesEvent ( event . loader , cls , isTestMethod ) <EOL> result = self . session . hooks . getTestMethodNames ( evt ) <EOL> if evt . handled : <EOL> test_names = result or [ ] <EOL> else : <EOL> excluded . update ( evt . excludedNames ) <EOL> test_names = [ entry for entry in dir ( cls ) <EOL> if isTestMethod ( entry ) ] <EOL> if event . loader . sortTestMethodsUsing : <EOL> test_names . sort ( key = event . loader . sortTestMethodsUsing ) <EOL> return test_names <EOL> def MethodTestCase ( cls ) : <EOL> class _MethodTestCase ( ut2 . TestCase ) : <EOL> def __init__ ( self , method ) : <EOL> self . method = method <EOL> self . _name = "<STR_LIT>" % ( cls . __module__ , cls . __name__ , method ) <EOL> self . obj = cls ( ) <EOL> ut2 . TestCase . __init__ ( self , '<STR_LIT>' ) <EOL> @ classmethod <EOL> def setUpClass ( klass ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . setUpClass ( ) <EOL> @ classmethod <EOL> def tearDownClass ( klass ) : <EOL> if hasattr ( cls , '<STR_LIT>' ) : <EOL> cls . tearDownClass ( ) <EOL> def setUp ( self ) : <EOL> if hasattr ( self . obj , '<STR_LIT>' ) : <EOL> self . obj . setUp ( ) <EOL> def tearDown ( self ) : <EOL> if hasattr ( self . obj , '<STR_LIT>' ) : <EOL> self . obj . tearDown ( ) <EOL> def __repr__ ( self ) : <EOL> return self . _name <EOL> id = __str__ = __repr__ <EOL> def runTest ( self ) : <EOL> getattr ( self . obj , self . method ) ( ) <EOL> return _MethodTestCase <EOL> class LoadFromTestClassEvent ( events . LoadFromTestCaseEvent ) : <EOL> """<STR_LIT>""" <EOL> class GetTestMethodNamesEvent ( events . GetTestCaseNamesEvent ) : <EOL> """<STR_LIT>""" </s>
