def _runTest(self, method, printTestName=False, numberOfAssertionFailed=0): exceptionRaised = False if printTestName: print '\t' + Colors.Cyan(method.__name__) try: if self.args.debug: raw_input( '\tenv is up, attach to any process with gdb and press any button to continue.' ) method() except unittest.SkipTest: print '\t' + Colors.Green('Skipping test') except Exception as err: msg = 'Unhandled exception: %s' % err print '\t' + Colors.Bred(msg) traceback.print_exc(file=sys.stdout) exceptionRaised = True isTestFaild = self.currEnv is None or self.currEnv.getNumberOfFailedAssertion( ) > numberOfAssertionFailed or exceptionRaised if isTestFaild: print '\t' + Colors.Bred('Test Failed') self.testsFailed.add(self.currEnv) else: print '\t' + Colors.Green('Test Passed') if self.args.stop_on_failure and isTestFaild: if self.args.interactive_debugger: while self.currEnv.isUp(): time.sleep(1) raw_input('press any button to move to the next test') return self.currEnv.getNumberOfFailedAssertion()
def _assertion(self, checkStr, trueValue, depth=0): if trueValue and self.verbose: print '\t' + Colors.Green('assertion success:\t') + Colors.Yellow( checkStr) + '\t' + Colors.Gray( self._getCallerPosition(3 + depth)) elif not trueValue: FailureSummery = Colors.Bred('assertion faild:\t') + Colors.Yellow( checkStr) + '\t' + Colors.Gray( self._getCallerPosition(3 + depth)) print '\t' + FailureSummery self.assertionFailedSummery.append(FailureSummery)
def _assertion(self, checkStr, trueValue, depth=0): basemsg = Colors.Yellow(checkStr) + '\t' + Colors.Gray( self._getCallerPosition(3 + depth)) if trueValue and self.verbose: print '\t' + Colors.Green('✅ (OK):\t') + basemsg elif not trueValue: failureSummary = Colors.Bred('❌ (FAIL):\t') + basemsg print '\t' + failureSummary if self.defaultExitOnFailure: raise TestAssertionFailure('Assertion Failed!') self.assertionFailedSummary.append(failureSummary)
def execute(self): self.testsFailed = set() Env.RTestInstance = self if self.args.env_only: Env.defaultVerbose = 2 env = Env(testName='manual test env') if self.args.interactive_debugger: while env.isUp(): time.sleep(1) raw_input('press any button to stop') env.stop() return if self.args.tests_file: self._loadFileTests(self.args.tests_file) else: self._loadTests() done = 0 startTime = time.time() if self.args.interactive_debugger and len(self.tests) != 1: print Colors.Bred( 'only one test can be run on interactive-debugger use --test-name' ) sys.exit(1) while self.tests: with self.envScopeGuard(): test = self.tests.pop(0) if inspect.isclass(test): # checking if there are tests to run methodsToTest = [] for m in dir(test): if self.args.test_name is not None: if self.args.test_name == m: methodsToTest.append(m) elif m.startswith('test') or m.startswith('Test'): methodsToTest.append(m) if len(methodsToTest) == 0: continue try: testObj = test() except unittest.SkipTest: print '\t' + Colors.Green('Skipping test') continue except Exception as err: msg = 'Unhandled exception: %s' % err print '\t' + Colors.Bred(msg) traceback.print_exc(file=sys.stdout) print '\t' + Colors.Bred('Test Failed') if self.currEnv: self.testsFailed.add(self.currEnv) continue methods = [ getattr(testObj, m) for m in dir(testObj) if callable(getattr(testObj, m)) and ( m.startswith('test') or m.startswith('Test')) ] numberOfAssertionFailed = 0 for m in methods: if self.args.test_name is None or self.args.test_name == m.__name__: numberOfAssertionFailed = self._runTest( m, printTestName=True, numberOfAssertionFailed=numberOfAssertionFailed ) done += 1 elif not inspect.isfunction(test): continue elif len(inspect.getargspec(test).args) > 0: env = Env(testName='%s.%s' % (str(test.__module__), test.func_name)) self._runTest(lambda: test(env)) done += 1 else: self._runTest(test) done += 1 self.takeEnvDown(fullShutDown=True) endTime = time.time() print Colors.Bold('Test Took: %d sec' % (endTime - startTime)) print Colors.Bold( 'Total Tests Run: %d, Total Tests Failed: %d, Total Tests Passed: %d' % (done, len(self.testsFailed), done - len(self.testsFailed))) if len(self.testsFailed) > 0: print Colors.Bold('Faild Tests Summery:') for testFaild in self.testsFailed: print '\t' + Colors.Bold(testFaild.testNamePrintable) testFaild.printFailuresSummery('\t\t') sys.exit(1)