def main(): args = parse_args() stream = subunit.ByteStreamToStreamResult(sys.stdin, non_subunit_name='stdout') outcomes = testtools.StreamToDict( functools.partial(show_outcome, sys.stdout, print_failures=args.print_failures, failonly=args.failonly)) summary = testtools.StreamSummary() result = testtools.CopyStreamResult([outcomes, summary]) result = testtools.StreamResultRouter(result) cat = subunit.test_results.CatFiles(sys.stdout) result.add_rule(cat, 'test_id', test_id=None) start_time = datetime.datetime.utcnow() result.startTestRun() try: stream.run(result) finally: result.stopTestRun() stop_time = datetime.datetime.utcnow() elapsed_time = stop_time - start_time if count_tests('status', '.*') == 0: print("The test run didn't actually run any tests") exit(1) if args.post_fails: print_fails(sys.stdout) print_summary(sys.stdout, elapsed_time) exit(0 if summary.wasSuccessful() else 1)
def main(): if len(sys.argv) < 2: print("Need at least one argument: path to subunit log.") exit(1) subunit_file = sys.argv[1] if len(sys.argv) > 2: html_file = sys.argv[2] else: html_file = 'results.html' html_result = HtmlOutput(html_file) stream = open(subunit_file, 'rb') # Feed the subunit stream through both a V1 and V2 parser. # Depends on having the v2 capable libraries installed. # First V2. # Non-v2 content and captured non-test output will be presented as file # segments called stdout. suite = subunit.ByteStreamToStreamResult(stream, non_subunit_name='stdout') # The HTML output code is in legacy mode. result = testtools.StreamToExtendedDecorator(html_result) # Divert non-test output accumulator = FileAccumulator() result = testtools.StreamResultRouter(result) result.add_rule(accumulator, 'test_id', test_id=None) result.startTestRun() suite.run(result) # Now reprocess any found stdout content as V1 subunit for bytes_io in accumulator.route_codes.values(): bytes_io.seek(0) suite = subunit.ProtocolTestCase(bytes_io) suite.run(html_result) result.stopTestRun()
def main(subunit_log_file): fd, results_file = tempfile.mkstemp() result = JsonOutput(results_file) stream = open(subunit_log_file, 'rb') # Feed the subunit stream through both a V1 and V2 parser. # Depends on having the v2 capable libraries installed. # First V2. # Non-v2 content and captured non-test output will be presented as file # segments called stdout. suite = subunit.ByteStreamToStreamResult(stream, non_subunit_name='stdout') # The JSON output code is in legacy mode. raw_result = testtools.StreamToExtendedDecorator(result) # Divert non-test output accumulator = FileAccumulator() result = testtools.StreamResultRouter(raw_result) result.add_rule(accumulator, 'test_id', test_id=None) result.startTestRun() suite.run(result) # Now reprocess any found stdout content as V1 subunit for bytes_io in accumulator.route_codes.values(): bytes_io.seek(0) suite = subunit.ProtocolTestCase(bytes_io) suite.run(result) result.stopTestRun() with open(results_file, 'rb') as temp_results_file: data = temp_results_file.read() try: os.unlink(results_file) except OSError as e: if e.errno != errno.ENOENT: raise return data
def wrap_result(result): # Wrap in a router to mask out startTestRun/stopTestRun from the # ExtendedToStreamDecorator. result = testtools.StreamResultRouter(result, do_start_stop_run=False) # Wrap that in ExtendedToStreamDecorator to convert v1 calls to # StreamResult. return testtools.ExtendedToStreamDecorator(result)
def _get_run_details(stream_file, stdout): stream = subunit.ByteStreamToStreamResult(stream_file, non_subunit_name='stdout') global start_times global stop_times start_times = [] stop_times = [] def collect_data(stream, test): global start_times global stop_times start_times.append(test['timestamps'][0]) stop_times.append(test['timestamps'][1]) outcomes = testtools.StreamToDict(functools.partial(collect_data, stdout)) summary = testtools.StreamSummary() result = testtools.CopyStreamResult([outcomes, summary]) result = testtools.StreamResultRouter(result) cat = subunit.test_results.CatFiles(stdout) result.add_rule(cat, 'test_id', test_id=None) result.startTestRun() try: stream.run(result) finally: result.stopTestRun() successful = results.wasSuccessful(summary) if start_times and stop_times: start_time = min(start_times) stop_time = max(stop_times) run_time = subunit_trace.get_duration([start_time, stop_time]) else: run_time = '---' successful = '---' start_time = '---' return {'passed': successful, 'runtime': run_time, 'start': start_time}
def trace(stdin, stdout, print_failures=False, failonly=False, enable_diff=False, abbreviate=False, color=False, post_fails=False, no_summary=False, suppress_attachments=False, all_attachments=False, show_binary_attachments=False): stream = subunit.ByteStreamToStreamResult(stdin, non_subunit_name='stdout') outcomes = testtools.StreamToDict( functools.partial(show_outcome, stdout, print_failures=print_failures, failonly=failonly, enable_diff=enable_diff, abbreviate=abbreviate, enable_color=color, suppress_attachments=suppress_attachments, all_attachments=all_attachments, show_binary_attachments=show_binary_attachments)) summary = testtools.StreamSummary() result = testtools.CopyStreamResult([outcomes, summary]) result = testtools.StreamResultRouter(result) cat = subunit.test_results.CatFiles(stdout) result.add_rule(cat, 'test_id', test_id=None) result.startTestRun() try: stream.run(result) finally: result.stopTestRun() start_times = [] stop_times = [] for worker in RESULTS: start_times += [x['timestamps'][0] for x in RESULTS[worker]] stop_times += [x['timestamps'][1] for x in RESULTS[worker]] start_time = min(start_times) stop_time = max(stop_times) elapsed_time = stop_time - start_time if count_tests('status', '.*') == 0: print("The test run didn't actually run any tests", file=sys.stderr) return 1 if post_fails: print_fails(stdout) if not no_summary: print_summary(stdout, elapsed_time) # NOTE(mtreinish): Ideally this should live in testtools streamSummary # this is just in place until the behavior lands there (if it ever does) if count_tests('status', '^success$') == 0: print("\nNo tests were successful during the run", file=sys.stderr) return 1 return 0 if results.wasSuccessful(summary) else 1
def main(): parser = make_options(__doc__) (options, args) = parser.parse_args() case = pysubunit.ByteStreamToStreamResult(filters.find_stream( sys.stdin, args), non_subunit_name='stdout') result = testtools.StreamToExtendedDecorator( pysubunit.decoratedTestProtocolClient(sys.stdout)) result = testtools.StreamResultRouter(result) cat = test_results.CatFiles(sys.stdout) result.add_rule(cat, 'test_id', test_id=None) result.startTestRun() case.run(result) result.stopTestRun() sys.exit(0)
def trace(stdin, stdout, print_failures=False, failonly=False, enable_diff=False, abbreviate=False, color=False, post_fails=False, no_summary=False): stream = subunit.ByteStreamToStreamResult(stdin, non_subunit_name='stdout') outcomes = testtools.StreamToDict( functools.partial(show_outcome, stdout, print_failures=print_failures, failonly=failonly, enable_diff=enable_diff, abbreviate=abbreviate, enable_color=color)) summary = testtools.StreamSummary() result = testtools.CopyStreamResult([outcomes, summary]) result = testtools.StreamResultRouter(result) cat = subunit.test_results.CatFiles(stdout) result.add_rule(cat, 'test_id', test_id=None) start_time = datetime.datetime.utcnow() result.startTestRun() try: stream.run(result) finally: result.stopTestRun() stop_time = datetime.datetime.utcnow() elapsed_time = stop_time - start_time if count_tests('status', '.*') == 0: print("The test run didn't actually run any tests") return 1 if post_fails: print_fails(stdout) if not no_summary: print_summary(stdout, elapsed_time) # NOTE(mtreinish): Ideally this should live in testtools streamSummary # this is just in place until the behavior lands there (if it ever does) if count_tests('status', '^success$') == 0: print("\nNo tests were successful during the run") return 1 return 0 if summary.wasSuccessful() else 1
def parse(subunit_file, non_subunit_name="pythonlogging"): subunit_parser = SubunitParser() stream = open(subunit_file, 'rb') suite = subunit.ByteStreamToStreamResult(stream, non_subunit_name=non_subunit_name) result = testtools.StreamToExtendedDecorator(subunit_parser) accumulator = FileAccumulator(non_subunit_name) result = testtools.StreamResultRouter(result) result.add_rule(accumulator, 'test_id', test_id=None) result.startTestRun() suite.run(result) for bytes_io in accumulator.route_codes.values(): # v1 processing bytes_io.seek(0) suite = subunit.ProtocolTestCase(bytes_io) suite.run(subunit_parser) result.stopTestRun() return subunit_parser
def parse(stream, non_subunit_name, ports): if ports is not None and os.path.exists(ports): ports = json.loads(open(ports).read()) url_parser = UrlParser(ports) suite = subunit.ByteStreamToStreamResult(stream, non_subunit_name=non_subunit_name) result = testtools.StreamToExtendedDecorator(url_parser) accumulator = FileAccumulator(non_subunit_name) result = testtools.StreamResultRouter(result) result.add_rule(accumulator, 'test_id', test_id=None) result.startTestRun() suite.run(result) for bytes_io in accumulator.route_codes.values(): # v1 processing bytes_io.seek(0) suite = subunit.ProtocolTestCase(bytes_io) suite.run(url_parser) result.stopTestRun() return url_parser
def main(): parser = optparse.OptionParser(description=__doc__) parser.add_option( "--times", action="store_true", help="list the time each test took (requires a timestamped stream)", default=False) parser.add_option( "--exists", action="store_true", help="list tests that are reported as existing (as well as ran)", default=False) parser.add_option("--no-passthrough", action="store_true", help="Hide all non subunit input.", default=False, dest="no_passthrough") (options, args) = parser.parse_args() test = pysubunit.ByteStreamToStreamResult(filters.find_stream( sys.stdin, args), non_subunit_name="stdout") result = test_results.TestIdPrintingResult(sys.stdout, options.times, options.exists) if not options.no_passthrough: result = testtools.StreamResultRouter(result) cat = test_results.CatFiles(sys.stdout) result.add_rule(cat, 'test_id', test_id=None) summary = testtools.StreamSummary() result = testtools.CopyStreamResult([result, summary]) result.startTestRun() test.run(result) result.stopTestRun() if summary.wasSuccessful(): exit_code = 0 else: exit_code = 1 sys.exit(exit_code)
def _load_case(inserter, repo, case, subunit_out, pretty_out, color, stdout, abbreviate, suppress_attachments, all_attachments): if subunit_out: output_result, summary_result = output.make_result(inserter.get_id, output=stdout) elif pretty_out: outcomes = testtools.StreamToDict( functools.partial(subunit_trace.show_outcome, stdout, enable_color=color, abbreviate=abbreviate, suppress_attachments=suppress_attachments, all_attachments=all_attachments)) summary_result = testtools.StreamSummary() output_result = testtools.CopyStreamResult([outcomes, summary_result]) output_result = testtools.StreamResultRouter(output_result) cat = subunit.test_results.CatFiles(stdout) output_result.add_rule(cat, 'test_id', test_id=None) else: try: previous_run = repo.get_latest_run() except KeyError: previous_run = None output_result = results.CLITestResult(inserter.get_id, stdout, previous_run) summary_result = output_result.get_summary() result = testtools.CopyStreamResult([inserter, output_result]) result.startTestRun() try: case.run(result) finally:
def run_tests_from_stream(input_stream, result, passthrough_stream=None, forward_stream=None, protocol_version=1, passthrough_subunit=True): """Run tests from a subunit input stream through 'result'. Non-test events - top level file attachments - are expected to be dropped by v2 StreamResults at the present time (as all the analysis code is in ExtendedTestResult API's), so to implement passthrough_stream they are diverted and copied directly when that is set. :param input_stream: A stream containing subunit input. :param result: A TestResult that will receive the test events. NB: This should be an ExtendedTestResult for v1 and a StreamResult for v2. :param passthrough_stream: All non-subunit input received will be sent to this stream. If not provided, uses the ``TestProtocolServer`` default, which is ``sys.stdout``. :param forward_stream: All subunit input received will be forwarded to this stream. If not provided, uses the ``TestProtocolServer`` default, which is to not forward any input. Do not set this when transforming the stream - items would be double-reported. :param protocol_version: What version of the subunit protocol to expect. :param passthrough_subunit: If True, passthrough should be as subunit otherwise unwrap it. Only has effect when forward_stream is None. (when forwarding as subunit non-subunit input is always turned into subunit) """ if 1 == protocol_version: test = pysubunit.ProtocolTestCase(input_stream, passthrough=passthrough_stream, forward=forward_stream) elif 2 == protocol_version: # In all cases we encapsulate unknown inputs. if forward_stream is not None: # Send events to forward_stream as subunit. forward_result = v2.StreamResultToBytes(forward_stream) # If we're passing non-subunit through, copy: if passthrough_stream is None: # Not passing non-test events - split them off to nothing. router = testtools.StreamResultRouter(forward_result) router.add_rule(testtools.StreamResult(), 'test_id', test_id=None) result = testtools.CopyStreamResult([router, result]) else: # otherwise, copy all events to forward_result result = testtools.CopyStreamResult([forward_result, result]) elif passthrough_stream is not None: # Route non-test events to passthrough_stream, unwrapping them for # display. if not passthrough_subunit: passthrough_result = test_results.CatFiles(passthrough_stream) else: passthrough_result = v2.StreamResultToBytes(passthrough_stream) result = testtools.StreamResultRouter(result) result.add_rule(passthrough_result, 'test_id', test_id=None) test = v2.ByteStreamToStreamResult(input_stream, non_subunit_name='stdout') else: raise Exception("Unknown protocol version.") result.startTestRun() test.run(result) result.stopTestRun()