def _run_case(self, test_name, suite_number, case, case_number): """A wrapper for case.run(). Prints informative output and also captures output of the test case and returns it as a log. The output is printed only if the case fails, or if self.verbose is True. """ output.off() # Delay printing until case status is determined. log_id = output.new_log() format.print_line('-') print('{} > Suite {} > Case {}'.format(test_name, suite_number, case_number)) print() success = case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) if not success or self.verbose: print(''.join(output_log)) if not success: short_name = self.test.get_short_name() # TODO: Change when in notebook mode print('Run only this test case with ' '"python3 ok -q {} --suite {} --case {}"'.format( short_name, suite_number, case_number)) return success
def run(self): """Runs the suites associated with this doctest. RETURNS: bool; True if the doctest completely passes, False otherwise. """ output.off() log_id = output.new_log() format.print_line('-') print('Doctests for {}'.format(self.name)) print() if not self.docstring: print('-- No doctests found for {} --'.format(self.name)) success = False else: success = self.case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) if not success or self.verbose: print(''.join(output_log)) if not success and self.interactive: self.console.interact() if success: return {'passed': 1, 'failed': 0, 'locked': 0} else: return {'passed': 0, 'failed': 1, 'locked': 0}
def _run_tests(self): """Run a read-eval loop that reads from src_file and collects outputs.""" if not self._import_scheme(): return 0, 0 output.off() reader = None try: reader = TestReader(self.file_contents.split('\n')) src = self.scheme.Buffer(self.scheme.tokenize_lines(reader)) def next_line(): src.current() if reader.line_number == len(reader.lines): # No more lines in file. raise EOFError return src timer.timed(self.timeout, self.scheme.read_eval_print_loop, (next_line, self.scheme.create_global_frame())) except BaseException as e: if reader: print("Tests terminated due to unhandled exception " "after line {}:\n" "{}: {}".format(reader.line_number, e.__class__.__name__, e)) output.on() if reader: return self._summarize(reader.output, reader.expected_output) return 0, 0
def run(self): """Runs the suites associated with this doctest. RETURNS: bool; True if the doctest completely passes, False otherwise. """ output.off() log_id = output.new_log() format.print_line('-') print('Doctests for {}'.format(self.name)) print() success = self.case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) if not success or self.verbose: print(''.join(output_log)) if not success and self.interactive: self.console.interact() if success: return {'passed': 1, 'failed': 0, 'locked': 0} else: return {'passed': 0, 'failed': 1, 'locked': 0}
def testRegisterLog_oneLog_outputOn(self): output.on() log_id = output.new_log() print(self.MESSAGE1) print(self.MESSAGE2) log = output.get_log(log_id) output.remove_log(log_id) self.assertEqual([self.MESSAGE1, "\n", self.MESSAGE2, "\n"], log)
def testRegisterLog_manyLogs_outputOn(self): output.on() log_id1 = output.new_log() log_id2 = output.new_log() print(self.MESSAGE1) log1 = output.get_log(log_id1) log2 = output.get_log(log_id2) output.remove_log(log_id1) self.assertEqual([self.MESSAGE1, "\n"], log1) self.assertEqual([self.MESSAGE1, "\n"], log2) print(self.MESSAGE2) log2 = output.get_log(log_id2) output.remove_log(log_id2) self.assertEqual([self.MESSAGE1, "\n"], log1) self.assertEqual([self.MESSAGE1, "\n", self.MESSAGE2, "\n"], log2)
def run(self, env): """Runs the suites associated with this doctest. NOTE: env is intended only for use with the programmatic API to support Python OK tests. It is not used here. RETURNS: bool; True if the doctest completely passes, False otherwise. """ output.off() log_id = output.new_log() format.print_line('-') print('Doctests for {}'.format(self.name)) print() if not self.docstring: print('-- No doctests found for {} --'.format(self.name)) if self.ignore_empty: success = True else: success = False else: success = self.case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) if not success or self.verbose: print(''.join(output_log)) if not success and self.interactive: self.console.interact() if success: return {'passed': 1, 'failed': 0, 'locked': 0} else: return {'passed': 0, 'failed': 1, 'locked': 0}
class Suite(core.Serializable): type = core.String() scored = core.Boolean(default=True) cases = core.List() def __init__(self, verbose, interactive, timeout=None, **fields): super().__init__(**fields) self.verbose = verbose self.interactive = interactive self.timeout = timeout def run(self, test_name, suite_number): """Subclasses should override this method to run tests. PARAMETERS: test_name -- str; name of the parent test. suite_number -- int; suite number, assumed to be 1-indexed. RETURNS: dict; results of the following form: { 'passed': int, 'failed': int, 'locked': int, } """ raise NotImplementedError def _run_case(self, test_name, suite_number, case, case_number): """A wrapper for case.run(). Prints informative output and also captures output of the test case and returns it as a log. The output is suppressed -- it is up to the calling function to decide whether or not to print the log. """ output.off() # Delay printing until case status is determined. log_id = output.new_log() format.print_line('-') print('{} > Suite {} > Case {}'.format(test_name, suite_number, case_number)) print() success = case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) return success, output_log
def _run_case(self, test_name, suite_number, case, case_number): """A wrapper for case.run(). Prints informative output and also captures output of the test case and returns it as a log. The output is suppressed -- it is up to the calling function to decide whether or not to print the log. """ output.off() # Delay printing until case status is determined. log_id = output.new_log() format.print_line('-') print('{} > Suite {} > Case {}'.format(test_name, suite_number, case_number)) print() success = case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) return success, output_log
def setUp(self): self.old_stdout = sys.stdout self.old_logger = output._logger sys.stdout = output._logger = output._OutputLogger() output.on()
def setUp(self): sys.stdout = LOGGER output.on()
def tearDown(self): output.on() output.remove_all_logs() sys.stdout = sys.__stdout__
class Suite(core.Serializable): type = core.String() scored = core.Boolean(default=True) cases = core.List() def __init__(self, test, verbose, interactive, timeout=None, **fields): super().__init__(**fields) self.test = test self.verbose = verbose self.interactive = interactive self.timeout = timeout self.run_only = [] def run(self, test_name, suite_number, env=None): """Subclasses should override this method to run tests. PARAMETERS: test_name -- str; name of the parent test. suite_number -- int; suite number, assumed to be 1-indexed. env -- dict; used by programmatic API to provide a custom environment to run tests with. RETURNS: dict; results of the following form: { 'passed': int, 'failed': int, 'locked': int, } """ raise NotImplementedError def enumerate_cases(self): enumerated = enumerate(self.cases) if self.run_only: return [x for x in enumerated if x[0] + 1 in self.run_only] return enumerated def _run_case(self, test_name, suite_number, case, case_number): """A wrapper for case.run(). Prints informative output and also captures output of the test case and returns it as a log. The output is printed only if the case fails, or if self.verbose is True. """ output.off() # Delay printing until case status is determined. log_id = output.new_log() format.print_line('-') print('{} > Suite {} > Case {}'.format(test_name, suite_number, case_number)) print() success = case.run() if success: print('-- OK! --') output.on() output_log = output.get_log(log_id) output.remove_log(log_id) if not success or self.verbose: print(''.join(output_log)) if not success: short_name = self.test.get_short_name() # TODO: Change when in notebook mode print('Run only this test case with ' '"python3 ok -q {} --suite {} --case {}"'.format( short_name, suite_number, case_number)) return success