def _run_tests(self, tag, stage=None):
        """Run test codes, defined in the test.py file, at event points"""
        import inspect
        import unittest
        from ambry.bundle.events import _runable_for_event
        import StringIO
        from ambry.util import delete_module

        suite = unittest.TestSuite()

        test_class = self.import_tests()

        funcs = inspect.getmembers(test_class, predicate=inspect.ismethod)

        tests = []

        for func_name, f in funcs:
            if _runable_for_event(f, tag, stage):
                tests.append(test_class(f.__name__))


        if tests:
            suite.addTests(tests)
            stream = StringIO.StringIO()

            r = unittest.TextTestRunner(stream=stream, verbosity=2).run(suite)

            # self.log(stream.getvalue())

            self.log("Ran {} tests for tag '{}', {}{} failed, {} errors, {} skipped".format(
                r.testsRun, tag, ' stage {}, '.format(stage) if stage is not None else '',
                len(r.failures), len(r.errors), len(r.skipped)
            ))

            for test, trace in r.failures:
                self.error('Test Failure: {} {}'.format(test, trace))

            for test, trace in r.errors:
                self.error('Test Error: {} {}'.format(test, trace))

            if len(r.failures) + len(r.errors) > 0:
                from ambry.dbexceptions import TestError
                self.set_error_state()
                self.buildstate.state.test_error = [
                    (str(test), str(trc)) for test, trc in r.failures + r.errors
                    ]
                raise TestError('Failed tests: {}'
                                .format(', '.join([str(test) for test, trc in r.failures + r.errors])))

        # Delete the Test class, because  otherwise in test suites it can get run
        # by a subsequent bundle.


        try:
            delete_module('ambry.build.test')
        except ValueError:
            pass
def _run_tests ( self , tag , stage = None ) : import inspect import unittest from ambry . bundle . events import _runable_for_event import StringIO from ambry . util import delete_module suite = unittest . TestSuite ( ) test_class = self . import_tests ( ) funcs = inspect . getmembers ( test_class , predicate = inspect . ismethod ) tests = [ ] for func_name , f in funcs : if _runable_for_event ( f , tag , stage ) : tests . append ( test_class ( f . __name__ ) ) if tests : suite . addTests ( tests ) stream = StringIO . StringIO ( ) r = unittest . TextTestRunner ( stream = stream , verbosity = 2 ) . run ( suite ) self . log ( "Ran {} tests for tag '{}', {}{} failed, {} errors, {} skipped" . format ( r . testsRun , tag , ' stage {}, ' . format ( stage ) if stage is not None else '' , len ( r . failures ) , len ( r . errors ) , len ( r . skipped ) ) ) for test , trace in r . failures : self . error ( 'Test Failure: {} {}' . format ( test , trace ) ) for test , trace in r . errors : self . error ( 'Test Error: {} {}' . format ( test , trace ) ) if len ( r . failures ) + len ( r . errors ) > 0 : from ambry . dbexceptions import TestError self . set_error_state ( ) self . buildstate . state . test_error = [ ( str ( test ) , str ( trc ) ) for test , trc in r . failures + r . errors ] raise TestError ( 'Failed tests: {}' . format ( ', ' . join ( [ str ( test ) for test , trc in r . failures + r . errors ] ) ) ) try : delete_module ( 'ambry.build.test' ) except ValueError : pass
Run test codes, defined in the test.py file, at event points