Exemple #1
0
def subunit2junitxml(ctr):
    from testtools import ExtendedToStreamDecorator
    from testtools import StreamToExtendedDecorator

    from subunit import StreamResultToBytes
    from subunit.filters import filter_by_result
    from subunit.filters import run_tests_from_stream

    from spyne.util.six import BytesIO

    from junitxml import JUnitXmlResult

    sys.argv = ['subunit-1to2']
    subunit1_file_name = 'test_result.%d.subunit' % ctr

    subunit2 = BytesIO()
    run_tests_from_stream(open(subunit1_file_name, 'rb'),
                          ExtendedToStreamDecorator(
                              StreamResultToBytes(subunit2)))
    subunit2.seek(0)

    sys.argv = ['subunit2junitxml']
    sys.stdin = subunit2

    def f(output):
        return StreamToExtendedDecorator(JUnitXmlResult(output))

    junit_file_name = 'test_result.%d.xml' % ctr

    filter_by_result(f, junit_file_name, True, False, protocol_version=2,
                     passthrough_subunit=True, input_stream=subunit2)
Exemple #2
0
def _construct_subunit(**kwargs):
    from subunit import StreamResultToBytes
    stream = kwargs.pop('stream')
    failfast = kwargs.pop('failfast')
    _raise_on_unknown_kwargs(kwargs)
    result_object = LoggedTestResultDecorator(
        ExtendedToStreamDecorator(StreamResultToBytes(stream)))
    result_object.failfast = failfast
    return result_object
Exemple #3
0
 def test_default(self):
     byte_stream = BytesIO()
     stream = StreamResultToBytes(byte_stream)
     stream.status(test_id="foo", test_status="inprogress")
     stream.status(test_id="foo", test_status="skip")
     output = self.run_command([], byte_stream.getvalue())
     events = StreamResult()
     ByteStreamToStreamResult(BytesIO(output)).run(events)
     ids = set(event[1] for event in events._events)
     self.assertEqual([
         ('status', 'foo', 'inprogress'),
         ('status', 'foo', 'skip'),
     ], [event[:3] for event in events._events])
Exemple #4
0
 def _list(self, test):
     test_ids, errors = list_test(test)
     try:
         fileno = self.stream.fileno()
     except:
         fileno = None
     if fileno is not None:
         stream = os.fdopen(fileno, 'wb', 0)
     else:
         stream = self.stream
     result = StreamResultToBytes(stream)
     for test_id in test_ids:
         result.status(test_id=test_id, test_status='exists')
     return result, errors
Exemple #5
0
 def test_tags(self):
     byte_stream = BytesIO()
     stream = StreamResultToBytes(byte_stream)
     stream.status(test_id="foo",
                   test_status="inprogress",
                   test_tags=set(["a"]))
     stream.status(test_id="foo",
                   test_status="success",
                   test_tags=set(["a"]))
     stream.status(test_id="bar", test_status="inprogress")
     stream.status(test_id="bar", test_status="inprogress")
     stream.status(test_id="baz",
                   test_status="inprogress",
                   test_tags=set(["a"]))
     stream.status(test_id="baz",
                   test_status="success",
                   test_tags=set(["a"]))
     output = self.run_command(['-s', '--with-tag', 'a'],
                               byte_stream.getvalue())
     events = StreamResult()
     ByteStreamToStreamResult(BytesIO(output)).run(events)
     ids = set(event[1] for event in events._events)
     self.assertEqual(set(['foo', 'baz']), ids)
Exemple #6
0
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 = 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 = 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 = StreamResultRouter(forward_result)
                router.add_rule(StreamResult(), 'test_id', test_id=None)
                result = CopyStreamResult([router, result])
            else:
                # otherwise, copy all events to forward_result
                result = CopyStreamResult([forward_result, result])
        elif passthrough_stream is not None:
            if not passthrough_subunit:
                # Route non-test events to passthrough_stream, unwrapping them for
                # display.
                passthrough_result = CatFiles(passthrough_stream)
            else:
                passthrough_result = StreamResultToBytes(passthrough_stream)
            result = StreamResultRouter(result)
            result.add_rule(passthrough_result, 'test_id', test_id=None)
        test = ByteStreamToStreamResult(input_stream,
                                        non_subunit_name='stdout')
    else:
        raise Exception("Unknown protocol version.")
    result.startTestRun()
    test.run(result)
    result.stopTestRun()
Exemple #7
0
 def test_passthrough(self):
     output = self.run_command([], b'hi thar')
     byte_stream = BytesIO()
     stream = StreamResultToBytes(byte_stream)
     stream.status(file_name="stdout", file_bytes=b'hi thar')
     self.assertEqual(byte_stream.getvalue(), output)