def assertOutput(self, template, kind, kwargs=None):
        """Assert the expected output from a run for a given test.

        :param template: A string where common strings have been replaced by a
            keyword so we don't run into pep8 warnings for long lines.

        :param kind: A string used to select the kind of test.

        :param kwargs: A dict with more keywords for the template. This allows
            some tests to add more keywords when they are test specific.
        """
        test = tests.get_case(kind)
        # Get subunit output (what subprocess produce)
        stream = StringIO()
        res = subunit.TestProtocolClient(stream)
        test.run(res)
        # Inject it again (what controlling process consumes)
        receiver = subunit.ProtocolTestCase(StringIO(stream.getvalue()))
        out = StringIO()
        res = junitxml.JUnitXmlResult(out)
        # We don't care about timing here so we always return 0 which
        # simplifies matching the expected result
        res._now = lambda: 0.0
        res._duration = lambda f: 0.0
        expected = tests.expand_template_for_test(template, test, kwargs)
        res.startTestRun()
        receiver.run(res)
        # due to the nature of JUnit XML output, nothing will be written to
        # the stream until stopTestRun() is called.
        res.stopTestRun()
        self.assertEquals(expected, out.getvalue())
Beispiel #2
0
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()
Beispiel #3
0
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 process_stream(self):
        with open(self.in_stream, 'r') as fin:
            test = subunit.ProtocolTestCase(fin)
            runner = unittest.TextTestRunner(stream=open(os.devnull, 'w'),
                                             resultclass=self.result_class)

            # Run (replay) the test from subunit stream.
            test_result = runner.run(test)
            return test_result.get_results()
    def run_tests(self, result_filter, input_stream=None):
        """Run tests through the given filter.

        :param result_filter: A filtering TestResult object.
        :param input_stream: Bytes of subunit stream data. If not provided,
            uses TestTestResultFilter.example_subunit_stream.
        """
        if input_stream is None:
            input_stream = self.example_subunit_stream
        test = subunit.ProtocolTestCase(BytesIO(input_stream))
        test.run(result_filter)
Beispiel #6
0
    def __init__(self,
                 in_stream,
                 test_result_class_name=TempestSubunitTestResult):
        """Read and process subunit data from a stream.

        Save processed data into a class named TempestSubunitTestResult
        which is a class derived from unittest.TestResults.
        """
        test = subunit.ProtocolTestCase(in_stream, passthrough=None)
        runner = unittest.TextTestRunner(verbosity=2,
                                         resultclass=test_result_class_name)
        #Run (replay) the test from subunit stream.
        #runner,run will return an object of type "test_result_class_name"
        self.result = runner.run(test)
    def assertOutput(self, expected, kind):
        test = tests.get_case(kind)
        # Get subunit output (what subprocess produce)
        stream = StringIO()
        res = subunit.TestProtocolClient(stream)
        test.run(res)
        # Inject it again (what controlling process consumes)
        receiver = subunit.ProtocolTestCase(StringIO(stream.getvalue()))
        out = StringIO()
        text_result = results.TextTestResult(out, verbosity=0)

        # We don't care about timing here so we always return 0 which
        # simplifies matching the expected result
        def zero(atime):
            return 0.0

        text_result._delta_to_float = zero
        receiver.run(text_result)
        self.assertEquals(expected, out.getvalue())
Beispiel #8
0
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
Beispiel #10
0
    def printErrors(self):
        if self.showAll:
            self.stream.writeln()
        self.printErrorList('ERROR', self.errors)
        self.printErrorList('FAIL', self.failures)

    def printErrorList(self, flavor, errors):
        for test, err in errors:
            self.colorizer.write("=" * 70, 'red')
            self.stream.writeln()
            self.colorizer.write(flavor, 'red')
            self.stream.writeln(": %s" % test.id())
            self.colorizer.write("-" * 70, 'red')
            self.stream.writeln()
            self.stream.writeln("%s" % err)


test = subunit.ProtocolTestCase(sys.stdin, passthrough=None)

if sys.version_info[0:2] <= (2, 6):
    runner = unittest.TextTestRunner(verbosity=2)
else:
    runner = unittest.TextTestRunner(verbosity=2, resultclass=NovaTestResult)

if runner.run(test).wasSuccessful():
    exit_code = 0
else:
    exit_code = 1
sys.exit(exit_code)
Beispiel #11
0
 def setUp(self):
     self.output = StringIO()
     self.result = subunit.TestResultStats(self.output)
     self.input_stream = BytesIO()
     self.test = subunit.ProtocolTestCase(self.input_stream)
Beispiel #12
0
 def to_events(self, stream):
     test = subunit.ProtocolTestCase(BytesIO(stream))
     result = ExtendedTestResult()
     test.run(result)
     return result._events
 def run_tests(self):
     self.setUpTestStream()
     self.test = subunit.ProtocolTestCase(self.input_stream)
     self.test.run(self.filter)
Beispiel #14
0
 def get_test(self):
     return subunit.ProtocolTestCase(self.get_subunit_stream())