class TestTestProtocolServerProgress(unittest.TestCase): """Test receipt of progress: directives.""" def test_progress_accepted_stdlib(self): self.result = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("progress: 23")) self.protocol.lineReceived(_b("progress: -2")) self.protocol.lineReceived(_b("progress: +4")) self.assertEqual(_b(""), self.stream.getvalue()) def test_progress_accepted_extended(self): # With a progress capable TestResult, progress events are emitted. self.result = ExtendedTestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("progress: 23")) self.protocol.lineReceived(_b("progress: push")) self.protocol.lineReceived(_b("progress: -2")) self.protocol.lineReceived(_b("progress: pop")) self.protocol.lineReceived(_b("progress: +4")) self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual([ ('progress', 23, subunit.PROGRESS_SET), ('progress', None, subunit.PROGRESS_PUSH), ('progress', -2, subunit.PROGRESS_CUR), ('progress', None, subunit.PROGRESS_POP), ('progress', 4, subunit.PROGRESS_CUR), ], self.result._events)
def test_time_accepted_stdlib(self): self.result = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue())
def test_includes_timing_output(self): io = BytesIO() runner = SubunitTestRunner(stream=io) test = PlaceHolder('name') runner.run(test) client = TimeCollectingTestResult() io.seek(0) subunit.TestProtocolServer(client).readFrom(io) self.assertTrue(len(client.time_called) > 0)
def test_progress_accepted_stdlib(self): self.result = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("progress: 23")) self.protocol.lineReceived(_b("progress: -2")) self.protocol.lineReceived(_b("progress: +4")) self.assertEqual(_b(""), self.stream.getvalue())
def test_not_command(self): client = unittest.TestResult() out = BytesIO() protocol = subunit.TestProtocolServer(client, stream=subunit.DiscardStream(), forward_stream=out) pipe = BytesIO(_b("success old mcdonald\n")) protocol.readFrom(pipe) self.assertEqual(client.testsRun, 0) self.assertEqual(_b(""), out.getvalue())
def test_story(self): client = unittest.TestResult() out = BytesIO() protocol = subunit.TestProtocolServer(client, forward_stream=out) pipe = BytesIO(_b("test old mcdonald\n" "success old mcdonald\n")) protocol.readFrom(pipe) self.assertEqual(client.testsRun, 1) self.assertEqual(pipe.getvalue(), out.getvalue())
def test_includes_timing_output(self): io = BytesIO() runner = SubunitTestRunner(stream=io) test = PlaceHolder('name') runner.run(test) io.seek(0) eventstream = StreamResult() subunit.ByteStreamToStreamResult(io).run(eventstream) timestamps = [event[-1] for event in eventstream._events if event is not None] self.assertNotEqual([], timestamps)
def test_time_accepted_extended(self): self.result = ExtendedTestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual( [('time', datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.Utc()))], self.result._events)
def test_includes_timing_output(self): io = BytesIO() runner = SubunitTestRunner(stream=io) test = PlaceHolder('name') runner.run(test) io.seek(0) eventstream = StreamResult() subunit.ByteStreamToStreamResult(io).run(eventstream) timestamps = [ event[-1] for event in eventstream._events if event is not None ] self.assertNotEqual([], timestamps)
def setUp(self): self.io = BytesIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test") self.sample_details = { 'something': Content(ContentType('text', 'plain'), lambda: [_b('serialised\nform')]) } self.sample_tb_details = dict(self.sample_details) self.sample_tb_details['traceback'] = TracebackContent( subunit.RemoteError(_u("boo qux")), self.test)
class TestTestResultStats(unittest.TestCase): """Test for TestResultStats, a TestResult object that generates stats.""" def setUp(self): self.output = StringIO() self.result = subunit.TestResultStats(self.output) self.input_stream = BytesIO() self.test = subunit.ProtocolTestCase(self.input_stream) def test_stats_empty(self): self.test.run(self.result) self.assertEqual(0, self.result.total_tests) self.assertEqual(0, self.result.passed_tests) self.assertEqual(0, self.result.failed_tests) self.assertEqual(set(), self.result.seen_tags) def setUpUsedStream(self): self.input_stream.write(_b("""tags: global test passed success passed test failed tags: local failure failed test error error error test skipped skip skipped test todo xfail todo """)) self.input_stream.seek(0) self.test.run(self.result) def test_stats_smoke_everything(self): # Statistics are calculated usefully. self.setUpUsedStream() self.assertEqual(5, self.result.total_tests) self.assertEqual(2, self.result.passed_tests) self.assertEqual(2, self.result.failed_tests) self.assertEqual(1, self.result.skipped_tests) self.assertEqual(set(["global", "local"]), self.result.seen_tags) def test_stat_formatting(self): expected = (""" Total tests: 5 Passed tests: 2 Failed tests: 2 Skipped tests: 1 Seen tags: global, local """)[1:] self.setUpUsedStream() self.result.formatStats() self.assertEqual(expected, self.output.getvalue())
class TestTestResultStats(unittest.TestCase): """Test for TestResultStats, a TestResult object that generates stats.""" def setUp(self): self.output = StringIO() self.result = subunit.TestResultStats(self.output) self.input_stream = BytesIO() self.test = subunit.ProtocolTestCase(self.input_stream) def test_stats_empty(self): self.test.run(self.result) self.assertEqual(0, self.result.total_tests) self.assertEqual(0, self.result.passed_tests) self.assertEqual(0, self.result.failed_tests) self.assertEqual(set(), self.result.seen_tags) def setUpUsedStream(self): self.input_stream.write( _b("""tags: global test passed success passed test failed tags: local failure failed test error error error test skipped skip skipped test todo xfail todo """)) self.input_stream.seek(0) self.test.run(self.result) def test_stats_smoke_everything(self): # Statistics are calculated usefully. self.setUpUsedStream() self.assertEqual(5, self.result.total_tests) self.assertEqual(2, self.result.passed_tests) self.assertEqual(2, self.result.failed_tests) self.assertEqual(1, self.result.skipped_tests) self.assertEqual(set(["global", "local"]), self.result.seen_tags) def test_stat_formatting(self): expected = (""" Total tests: 5 Passed tests: 2 Failed tests: 2 Skipped tests: 1 Seen tags: global, local """)[1:] self.setUpUsedStream() self.result.formatStats() self.assertEqual(expected, self.output.getvalue())
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])
def test_enumerates_tests_before_run(self): io = BytesIO() runner = SubunitTestRunner(stream=io) test1 = PlaceHolder('name1') test2 = PlaceHolder('name2') case = unittest.TestSuite([test1, test2]) runner.run(case) io.seek(0) eventstream = StreamResult() subunit.ByteStreamToStreamResult(io).run(eventstream) self.assertEqual([ ('status', 'name1', 'exists'), ('status', 'name2', 'exists'), ], [event[:3] for event in eventstream._events[:2]])
class TestTestProtocolServerStartTest(unittest.TestCase): def setUp(self): self.client = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.client, self.stream) def test_start_test(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.assertEqual(self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_start_testing(self): self.protocol.lineReceived(_b("testing old mcdonald\n")) self.assertEqual(self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_start_test_colon(self): self.protocol.lineReceived(_b("test: old mcdonald\n")) self.assertEqual(self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_indented_test_colon_ignored(self): ignored_line = _b(" test: old mcdonald\n") self.protocol.lineReceived(ignored_line) self.assertEqual([], self.client._events) self.assertEqual(self.stream.getvalue(), ignored_line) def test_start_testing_colon(self): self.protocol.lineReceived(_b("testing: old mcdonald\n")) self.assertEqual(self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))])
def test_from_stream_with_simple_seek(self): data = BytesIO(_b('some data')) content = content_from_stream(data, UTF8_TEXT, chunk_size=50, seek_offset=5) self.assertThat(list(content.iter_bytes()), Equals([_b('data')]))
def _run(self, result): protocol = TestProtocolServer(result) process = subprocess.Popen(self.script, shell=True, stdout=subprocess.PIPE) make_stream_binary(process.stdout) output = process.communicate()[0] protocol.readFrom(BytesIO(output))
class TestTestProtocolServerStartTest(unittest.TestCase): def setUp(self): self.client = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.client, self.stream) def test_start_test(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.assertEqual( self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_start_testing(self): self.protocol.lineReceived(_b("testing old mcdonald\n")) self.assertEqual( self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_start_test_colon(self): self.protocol.lineReceived(_b("test: old mcdonald\n")) self.assertEqual( self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))]) def test_indented_test_colon_ignored(self): ignored_line = _b(" test: old mcdonald\n") self.protocol.lineReceived(ignored_line) self.assertEqual([], self.client._events) self.assertEqual(self.stream.getvalue(), ignored_line) def test_start_testing_colon(self): self.protocol.lineReceived(_b("testing: old mcdonald\n")) self.assertEqual( self.client._events, [('startTest', subunit.RemotedTestCase("old mcdonald"))])
def setUp(self): self.io = BytesIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test") self.sample_details = {'something':Content( ContentType('text', 'plain'), lambda:[_b('serialised\nform')])} self.sample_tb_details = dict(self.sample_details) self.sample_tb_details['traceback'] = TracebackContent( subunit.RemoteError(_u("boo qux")), self.test)
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)
def test_test_module(self): self.useFixture(SampleTestFixture()) stream = BytesIO() dist = Distribution() dist.script_name = 'setup.py' dist.script_args = ['test'] dist.cmdclass = {'test': TestCommand} dist.command_options = { 'test': {'test_module': ('command line', 'testtools.runexample')}} cmd = dist.reinitialize_command('test') cmd.runner.stdout = stream dist.run_command('test') self.assertThat( stream.getvalue(), MatchesRegex(_b("""Tests running... Ran 2 tests in \\d.\\d\\d\\ds OK """)))
def test_list_errors_if_errors_from_list_test(self): io = BytesIO() runner = SubunitTestRunner(stream=io) def list_test(test): return [], ['failed import'] self.patch(run, 'list_test', list_test) exc = self.assertRaises(SystemExit, runner.list, None) self.assertEqual((2, ), exc.args)
def test_time_accepted_extended(self): self.result = ExtendedTestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual([ ('time', datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.Utc())) ], self.result._events)
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)
class TestTestProtocolServerStreamTime(unittest.TestCase): """Test managing time information at the protocol level.""" def test_time_accepted_stdlib(self): self.result = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) def test_time_accepted_extended(self): self.result = ExtendedTestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual( [('time', datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.Utc()))], self.result._events)
class TestTestProtocolServerStreamTime(unittest.TestCase): """Test managing time information at the protocol level.""" def test_time_accepted_stdlib(self): self.result = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) def test_time_accepted_extended(self): self.result = ExtendedTestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.result, stream=self.stream) self.protocol.lineReceived(_b("time: 2001-12-12 12:59:59Z\n")) self.assertEqual(_b(""), self.stream.getvalue()) self.assertEqual([ ('time', datetime.datetime(2001, 12, 12, 12, 59, 59, 0, iso8601.Utc())) ], self.result._events)
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)
class TestEncode(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.output = BytesIO() self.encoder = subunit.chunked.Encoder(self.output) def test_encode_nothing(self): self.encoder.close() self.assertEqual(_b("0\r\n"), self.output.getvalue()) def test_encode_empty(self): self.encoder.write(_b("")) self.encoder.close() self.assertEqual(_b("0\r\n"), self.output.getvalue()) def test_encode_short(self): self.encoder.write(_b("abc")) self.encoder.close() self.assertEqual(_b("3\r\nabc0\r\n"), self.output.getvalue()) def test_encode_combines_short(self): self.encoder.write(_b("abc")) self.encoder.write(_b("def")) self.encoder.close() self.assertEqual(_b("6\r\nabcdef0\r\n"), self.output.getvalue()) def test_encode_over_9_is_in_hex(self): self.encoder.write(_b("1234567890")) self.encoder.close() self.assertEqual(_b("A\r\n12345678900\r\n"), self.output.getvalue()) def test_encode_long_ranges_not_combined(self): self.encoder.write(_b("1" * 65536)) self.encoder.write(_b("2" * 65536)) self.encoder.close() self.assertEqual(_b("10000\r\n" + "1" * 65536 + "10000\r\n" + "2" * 65536 + "0\r\n"), self.output.getvalue())
def test_story(self): client = unittest.TestResult() protocol = subunit.TestProtocolServer(client) pipe = BytesIO( _b("test old mcdonald\n" "success old mcdonald\n" "test bing crosby\n" "failure bing crosby [\n" "foo.c:53:ERROR invalid state\n" "]\n" "test an error\n" "error an error\n")) protocol.readFrom(pipe) bing = subunit.RemotedTestCase("bing crosby") an_error = subunit.RemotedTestCase("an error") self.assertEqual(client.errors, [(an_error, _remote_exception_str + '\n')]) self.assertEqual( client.failures, [(bing, _remote_exception_str + ": Text attachment: traceback\n" "------------\nfoo.c:53:ERROR invalid state\n" "------------\n\n")]) self.assertEqual(client.testsRun, 3)
class TestEncode(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.output = BytesIO() self.encoder = subunit.chunked.Encoder(self.output) def test_encode_nothing(self): self.encoder.close() self.assertEqual(_b('0\r\n'), self.output.getvalue()) def test_encode_empty(self): self.encoder.write(_b('')) self.encoder.close() self.assertEqual(_b('0\r\n'), self.output.getvalue()) def test_encode_short(self): self.encoder.write(_b('abc')) self.encoder.close() self.assertEqual(_b('3\r\nabc0\r\n'), self.output.getvalue()) def test_encode_combines_short(self): self.encoder.write(_b('abc')) self.encoder.write(_b('def')) self.encoder.close() self.assertEqual(_b('6\r\nabcdef0\r\n'), self.output.getvalue()) def test_encode_over_9_is_in_hex(self): self.encoder.write(_b('1234567890')) self.encoder.close() self.assertEqual(_b('A\r\n12345678900\r\n'), self.output.getvalue()) def test_encode_long_ranges_not_combined(self): self.encoder.write(_b('1' * 65536)) self.encoder.write(_b('2' * 65536)) self.encoder.close() self.assertEqual( _b('10000\r\n' + '1' * 65536 + '10000\r\n' + '2' * 65536 + '0\r\n'), self.output.getvalue())
class TestEncode(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.output = BytesIO() self.encoder = subunit.chunked.Encoder(self.output) def test_encode_nothing(self): self.encoder.close() self.assertEqual(_b('0\r\n'), self.output.getvalue()) def test_encode_empty(self): self.encoder.write(_b('')) self.encoder.close() self.assertEqual(_b('0\r\n'), self.output.getvalue()) def test_encode_short(self): self.encoder.write(_b('abc')) self.encoder.close() self.assertEqual(_b('3\r\nabc0\r\n'), self.output.getvalue()) def test_encode_combines_short(self): self.encoder.write(_b('abc')) self.encoder.write(_b('def')) self.encoder.close() self.assertEqual(_b('6\r\nabcdef0\r\n'), self.output.getvalue()) def test_encode_over_9_is_in_hex(self): self.encoder.write(_b('1234567890')) self.encoder.close() self.assertEqual(_b('A\r\n12345678900\r\n'), self.output.getvalue()) def test_encode_long_ranges_not_combined(self): self.encoder.write(_b('1' * 65536)) self.encoder.write(_b('2' * 65536)) self.encoder.close() self.assertEqual(_b('10000\r\n' + '1' * 65536 + '10000\r\n' + '2' * 65536 + '0\r\n'), self.output.getvalue())
def to_events(self, stream): test = subunit.ProtocolTestCase(BytesIO(stream)) result = ExtendedTestResult() test.run(result) return result._events
def setUp(self): self.client = Python26TestResult() self.stream = BytesIO() self.protocol = subunit.TestProtocolServer(self.client, self.stream)
def test_communicate_with_input_and_stdin(self): stdin = BytesIO() proc = FakeProcess({}, {'stdin': stdin}) proc.communicate(input=_b("hello")) self.assertEqual(_b("hello"), stdin.getvalue())
class TestTestProtocolClient(unittest.TestCase): def setUp(self): self.io = BytesIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test") self.sample_details = {'something':Content( ContentType('text', 'plain'), lambda:[_b('serialised\nform')])} self.sample_tb_details = dict(self.sample_details) self.sample_tb_details['traceback'] = TracebackContent( subunit.RemoteError(_u("boo qux")), self.test) def test_start_test(self): """Test startTest on a TestProtocolClient.""" self.protocol.startTest(self.test) self.assertEqual(self.io.getvalue(), _b("test: %s\n" % self.test.id())) def test_stop_test(self): # stopTest doesn't output anything. self.protocol.stopTest(self.test) self.assertEqual(self.io.getvalue(), _b("")) def test_add_success(self): """Test addSuccess on a TestProtocolClient.""" self.protocol.addSuccess(self.test) self.assertEqual( self.io.getvalue(), _b("successful: %s\n" % self.test.id())) def test_add_success_details(self): """Test addSuccess on a TestProtocolClient with details.""" self.protocol.addSuccess(self.test, details=self.sample_details) self.assertEqual( self.io.getvalue(), _b("successful: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n]\n" % self.test.id())) def test_add_failure(self): """Test addFailure on a TestProtocolClient.""" self.protocol.addFailure( self.test, subunit.RemoteError(_u("boo qux"))) self.assertEqual( self.io.getvalue(), _b(('failure: %s [\n' + _remote_exception_str + ': boo qux\n]\n') % self.test.id())) def test_add_failure_details(self): """Test addFailure on a TestProtocolClient with details.""" self.protocol.addFailure( self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("failure: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_error(self): """Test stopTest on a TestProtocolClient.""" self.protocol.addError( self.test, subunit.RemoteError(_u("phwoar crikey"))) self.assertEqual( self.io.getvalue(), _b(('error: %s [\n' + _remote_exception_str + ": phwoar crikey\n" "]\n") % self.test.id())) def test_add_error_details(self): """Test stopTest on a TestProtocolClient with details.""" self.protocol.addError( self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("error: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_expected_failure(self): """Test addExpectedFailure on a TestProtocolClient.""" self.protocol.addExpectedFailure( self.test, subunit.RemoteError(_u("phwoar crikey"))) self.assertEqual( self.io.getvalue(), _b(('xfail: %s [\n' + _remote_exception_str + ": phwoar crikey\n" "]\n") % self.test.id())) def test_add_expected_failure_details(self): """Test addExpectedFailure on a TestProtocolClient with details.""" self.protocol.addExpectedFailure( self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("xfail: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_skip(self): """Test addSkip on a TestProtocolClient.""" self.protocol.addSkip( self.test, "Has it really?") self.assertEqual( self.io.getvalue(), _b('skip: %s [\nHas it really?\n]\n' % self.test.id())) def test_add_skip_details(self): """Test addSkip on a TestProtocolClient with details.""" details = {'reason':Content( ContentType('text', 'plain'), lambda:[_b('Has it really?')])} self.protocol.addSkip(self.test, details=details) self.assertEqual( self.io.getvalue(), _b("skip: %s [ multipart\n" "Content-Type: text/plain\n" "reason\n" "E\r\nHas it really?0\r\n" "]\n" % self.test.id())) def test_progress_set(self): self.protocol.progress(23, subunit.PROGRESS_SET) self.assertEqual(self.io.getvalue(), _b('progress: 23\n')) def test_progress_neg_cur(self): self.protocol.progress(-23, subunit.PROGRESS_CUR) self.assertEqual(self.io.getvalue(), _b('progress: -23\n')) def test_progress_pos_cur(self): self.protocol.progress(23, subunit.PROGRESS_CUR) self.assertEqual(self.io.getvalue(), _b('progress: +23\n')) def test_progress_pop(self): self.protocol.progress(1234, subunit.PROGRESS_POP) self.assertEqual(self.io.getvalue(), _b('progress: pop\n')) def test_progress_push(self): self.protocol.progress(1234, subunit.PROGRESS_PUSH) self.assertEqual(self.io.getvalue(), _b('progress: push\n')) def test_time(self): # Calling time() outputs a time signal immediately. self.protocol.time( datetime.datetime(2009,10,11,12,13,14,15, iso8601.Utc())) self.assertEqual( _b("time: 2009-10-11 12:13:14.000015Z\n"), self.io.getvalue()) def test_add_unexpected_success(self): """Test addUnexpectedSuccess on a TestProtocolClient.""" self.protocol.addUnexpectedSuccess(self.test) self.assertEqual( self.io.getvalue(), _b("uxsuccess: %s\n" % self.test.id())) def test_add_unexpected_success_details(self): """Test addUnexpectedSuccess on a TestProtocolClient with details.""" self.protocol.addUnexpectedSuccess(self.test, details=self.sample_details) self.assertEqual( self.io.getvalue(), _b("uxsuccess: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n]\n" % self.test.id()))
class TestDecode(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.output = BytesIO() self.decoder = subunit.chunked.Decoder(self.output) def test_close_read_length_short_errors(self): self.assertRaises(ValueError, self.decoder.close) def test_close_body_short_errors(self): self.assertEqual(None, self.decoder.write(_b('2\r\na'))) self.assertRaises(ValueError, self.decoder.close) def test_close_body_buffered_data_errors(self): self.assertEqual(None, self.decoder.write(_b('2\r'))) self.assertRaises(ValueError, self.decoder.close) def test_close_after_finished_stream_safe(self): self.assertEqual(None, self.decoder.write(_b('2\r\nab'))) self.assertEqual(_b(''), self.decoder.write(_b('0\r\n'))) self.decoder.close() def test_decode_nothing(self): self.assertEqual(_b(''), self.decoder.write(_b('0\r\n'))) self.assertEqual(_b(''), self.output.getvalue()) def test_decode_serialised_form(self): self.assertEqual(None, self.decoder.write(_b("F\r\n"))) self.assertEqual(None, self.decoder.write(_b("serialised\n"))) self.assertEqual(_b(''), self.decoder.write(_b("form0\r\n"))) def test_decode_short(self): self.assertEqual(_b(''), self.decoder.write(_b('3\r\nabc0\r\n'))) self.assertEqual(_b('abc'), self.output.getvalue()) def test_decode_combines_short(self): self.assertEqual(_b(''), self.decoder.write(_b('6\r\nabcdef0\r\n'))) self.assertEqual(_b('abcdef'), self.output.getvalue()) def test_decode_excess_bytes_from_write(self): self.assertEqual(_b('1234'), self.decoder.write(_b('3\r\nabc0\r\n1234'))) self.assertEqual(_b('abc'), self.output.getvalue()) def test_decode_write_after_finished_errors(self): self.assertEqual(_b('1234'), self.decoder.write(_b('3\r\nabc0\r\n1234'))) self.assertRaises(ValueError, self.decoder.write, _b('')) def test_decode_hex(self): self.assertEqual(_b(''), self.decoder.write(_b('A\r\n12345678900\r\n'))) self.assertEqual(_b('1234567890'), self.output.getvalue()) def test_decode_long_ranges(self): self.assertEqual(None, self.decoder.write(_b('10000\r\n'))) self.assertEqual(None, self.decoder.write(_b('1' * 65536))) self.assertEqual(None, self.decoder.write(_b('10000\r\n'))) self.assertEqual(None, self.decoder.write(_b('2' * 65536))) self.assertEqual(_b(''), self.decoder.write(_b('0\r\n'))) self.assertEqual(_b('1' * 65536 + '2' * 65536), self.output.getvalue()) def test_decode_newline_nonstrict(self): """Tolerate chunk markers with no CR character.""" # From <http://pad.lv/505078> self.decoder = subunit.chunked.Decoder(self.output, strict=False) self.assertEqual(None, self.decoder.write(_b('a\n'))) self.assertEqual(None, self.decoder.write(_b('abcdeabcde'))) self.assertEqual(_b(''), self.decoder.write(_b('0\n'))) self.assertEqual(_b('abcdeabcde'), self.output.getvalue()) def test_decode_strict_newline_only(self): """Reject chunk markers with no CR character in strict mode.""" # From <http://pad.lv/505078> self.assertRaises(ValueError, self.decoder.write, _b('a\n')) def test_decode_strict_multiple_crs(self): self.assertRaises(ValueError, self.decoder.write, _b('a\r\r\n')) def test_decode_short_header(self): self.assertRaises(ValueError, self.decoder.write, _b('\n'))
def setUp(self): self.output = StringIO() self.result = subunit.TestResultStats(self.output) self.input_stream = BytesIO() self.test = subunit.ProtocolTestCase(self.input_stream)
def setUp(self): unittest.TestCase.setUp(self) self.output = BytesIO() self.decoder = subunit.chunked.Decoder(self.output)
def setUp(self): self.stdout = BytesIO() self.test = subunit.RemotedTestCase("old mcdonald") self.client = ExtendedTestResult() self.protocol = subunit.TestProtocolServer(self.client, self.stdout)
def test_communicate_with_timeout(self): proc = FakeProcess({}, {'stdout': BytesIO(_b('foo'))}) self.assertEqual((_b('foo'), ''), proc.communicate(timeout=10))
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)
class TestTestProtocolClient(unittest.TestCase): def setUp(self): self.io = BytesIO() self.protocol = subunit.TestProtocolClient(self.io) self.test = TestTestProtocolClient("test_start_test") self.sample_details = { 'something': Content(ContentType('text', 'plain'), lambda: [_b('serialised\nform')]) } self.sample_tb_details = dict(self.sample_details) self.sample_tb_details['traceback'] = TracebackContent( subunit.RemoteError(_u("boo qux")), self.test) def test_start_test(self): """Test startTest on a TestProtocolClient.""" self.protocol.startTest(self.test) self.assertEqual(self.io.getvalue(), _b("test: %s\n" % self.test.id())) def test_stop_test(self): # stopTest doesn't output anything. self.protocol.stopTest(self.test) self.assertEqual(self.io.getvalue(), _b("")) def test_add_success(self): """Test addSuccess on a TestProtocolClient.""" self.protocol.addSuccess(self.test) self.assertEqual(self.io.getvalue(), _b("successful: %s\n" % self.test.id())) def test_add_success_details(self): """Test addSuccess on a TestProtocolClient with details.""" self.protocol.addSuccess(self.test, details=self.sample_details) self.assertEqual( self.io.getvalue(), _b("successful: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n]\n" % self.test.id())) def test_add_failure(self): """Test addFailure on a TestProtocolClient.""" self.protocol.addFailure(self.test, subunit.RemoteError(_u("boo qux"))) self.assertEqual( self.io.getvalue(), _b(('failure: %s [\n' + _remote_exception_str + ': boo qux\n]\n') % self.test.id())) def test_add_failure_details(self): """Test addFailure on a TestProtocolClient with details.""" self.protocol.addFailure(self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("failure: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_error(self): """Test stopTest on a TestProtocolClient.""" self.protocol.addError(self.test, subunit.RemoteError(_u("phwoar crikey"))) self.assertEqual( self.io.getvalue(), _b(('error: %s [\n' + _remote_exception_str + ": phwoar crikey\n" "]\n") % self.test.id())) def test_add_error_details(self): """Test stopTest on a TestProtocolClient with details.""" self.protocol.addError(self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("error: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_expected_failure(self): """Test addExpectedFailure on a TestProtocolClient.""" self.protocol.addExpectedFailure( self.test, subunit.RemoteError(_u("phwoar crikey"))) self.assertEqual( self.io.getvalue(), _b(('xfail: %s [\n' + _remote_exception_str + ": phwoar crikey\n" "]\n") % self.test.id())) def test_add_expected_failure_details(self): """Test addExpectedFailure on a TestProtocolClient with details.""" self.protocol.addExpectedFailure(self.test, details=self.sample_tb_details) self.assertEqual( self.io.getvalue(), _b(("xfail: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n" "Content-Type: text/x-traceback;charset=utf8,language=python\n" "traceback\n" + _remote_exception_str_chunked + ": boo qux\n0\r\n" "]\n") % self.test.id())) def test_add_skip(self): """Test addSkip on a TestProtocolClient.""" self.protocol.addSkip(self.test, "Has it really?") self.assertEqual( self.io.getvalue(), _b('skip: %s [\nHas it really?\n]\n' % self.test.id())) def test_add_skip_details(self): """Test addSkip on a TestProtocolClient with details.""" details = { 'reason': Content(ContentType('text', 'plain'), lambda: [_b('Has it really?')]) } self.protocol.addSkip(self.test, details=details) self.assertEqual( self.io.getvalue(), _b("skip: %s [ multipart\n" "Content-Type: text/plain\n" "reason\n" "E\r\nHas it really?0\r\n" "]\n" % self.test.id())) def test_progress_set(self): self.protocol.progress(23, subunit.PROGRESS_SET) self.assertEqual(self.io.getvalue(), _b('progress: 23\n')) def test_progress_neg_cur(self): self.protocol.progress(-23, subunit.PROGRESS_CUR) self.assertEqual(self.io.getvalue(), _b('progress: -23\n')) def test_progress_pos_cur(self): self.protocol.progress(23, subunit.PROGRESS_CUR) self.assertEqual(self.io.getvalue(), _b('progress: +23\n')) def test_progress_pop(self): self.protocol.progress(1234, subunit.PROGRESS_POP) self.assertEqual(self.io.getvalue(), _b('progress: pop\n')) def test_progress_push(self): self.protocol.progress(1234, subunit.PROGRESS_PUSH) self.assertEqual(self.io.getvalue(), _b('progress: push\n')) def test_time(self): # Calling time() outputs a time signal immediately. self.protocol.time( datetime.datetime(2009, 10, 11, 12, 13, 14, 15, iso8601.Utc())) self.assertEqual(_b("time: 2009-10-11 12:13:14.000015Z\n"), self.io.getvalue()) def test_add_unexpected_success(self): """Test addUnexpectedSuccess on a TestProtocolClient.""" self.protocol.addUnexpectedSuccess(self.test) self.assertEqual(self.io.getvalue(), _b("uxsuccess: %s\n" % self.test.id())) def test_add_unexpected_success_details(self): """Test addUnexpectedSuccess on a TestProtocolClient with details.""" self.protocol.addUnexpectedSuccess(self.test, details=self.sample_details) self.assertEqual( self.io.getvalue(), _b("uxsuccess: %s [ multipart\n" "Content-Type: text/plain\n" "something\n" "F\r\nserialised\nform0\r\n]\n" % self.test.id()))
class TestTestProtocolServerPassThrough(unittest.TestCase): def setUp(self): self.stdout = BytesIO() self.test = subunit.RemotedTestCase("old mcdonald") self.client = ExtendedTestResult() self.protocol = subunit.TestProtocolServer(self.client, self.stdout) def keywords_before_test(self): self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b("]\n")) self.assertEqual( self.stdout.getvalue(), _b("failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n")) def test_keywords_before_test(self): self.keywords_before_test() self.assertEqual(self.client._events, []) def test_keywords_after_error(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("error old mcdonald\n")) self.keywords_before_test() self.assertEqual([ ('startTest', self.test), ('addError', self.test, {}), ('stopTest', self.test), ], self.client._events) def test_keywords_after_failure(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure old mcdonald\n")) self.keywords_before_test() self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, {}), ('stopTest', self.test), ]) def test_keywords_after_success(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("success old mcdonald\n")) self.keywords_before_test() self.assertEqual([ ('startTest', self.test), ('addSuccess', self.test), ('stopTest', self.test), ], self.client._events) def test_keywords_after_test(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b("]\n")) self.protocol.lineReceived(_b("failure old mcdonald\n")) self.assertEqual( self.stdout.getvalue(), _b("test old mcdonald\n" "failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n")) self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, {}), ('stopTest', self.test), ]) def test_keywords_during_failure(self): # A smoke test to make sure that the details parsers have control # appropriately. self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure: old mcdonald [\n")) self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b(" ]\n")) self.protocol.lineReceived(_b("]\n")) self.assertEqual(self.stdout.getvalue(), _b("")) details = {} details['traceback'] = Content( ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda: [ _b("test old mcdonald\n" "failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n") ]) self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, details), ('stopTest', self.test), ]) def test_stdout_passthrough(self): """Lines received which cannot be interpreted as any protocol action should be passed through to sys.stdout. """ bytes = _b("randombytes\n") self.protocol.lineReceived(bytes) self.assertEqual(self.stdout.getvalue(), bytes)
class TestTestProtocolServerPassThrough(unittest.TestCase): def setUp(self): self.stdout = BytesIO() self.test = subunit.RemotedTestCase("old mcdonald") self.client = ExtendedTestResult() self.protocol = subunit.TestProtocolServer(self.client, self.stdout) def keywords_before_test(self): self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b("]\n")) self.assertEqual(self.stdout.getvalue(), _b("failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n")) def test_keywords_before_test(self): self.keywords_before_test() self.assertEqual(self.client._events, []) def test_keywords_after_error(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("error old mcdonald\n")) self.keywords_before_test() self.assertEqual([ ('startTest', self.test), ('addError', self.test, {}), ('stopTest', self.test), ], self.client._events) def test_keywords_after_failure(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure old mcdonald\n")) self.keywords_before_test() self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, {}), ('stopTest', self.test), ]) def test_keywords_after_success(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("success old mcdonald\n")) self.keywords_before_test() self.assertEqual([ ('startTest', self.test), ('addSuccess', self.test), ('stopTest', self.test), ], self.client._events) def test_keywords_after_test(self): self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b("]\n")) self.protocol.lineReceived(_b("failure old mcdonald\n")) self.assertEqual(self.stdout.getvalue(), _b("test old mcdonald\n" "failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n")) self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, {}), ('stopTest', self.test), ]) def test_keywords_during_failure(self): # A smoke test to make sure that the details parsers have control # appropriately. self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure: old mcdonald [\n")) self.protocol.lineReceived(_b("test old mcdonald\n")) self.protocol.lineReceived(_b("failure a\n")) self.protocol.lineReceived(_b("failure: a\n")) self.protocol.lineReceived(_b("error a\n")) self.protocol.lineReceived(_b("error: a\n")) self.protocol.lineReceived(_b("success a\n")) self.protocol.lineReceived(_b("success: a\n")) self.protocol.lineReceived(_b("successful a\n")) self.protocol.lineReceived(_b("successful: a\n")) self.protocol.lineReceived(_b(" ]\n")) self.protocol.lineReceived(_b("]\n")) self.assertEqual(self.stdout.getvalue(), _b("")) details = {} details['traceback'] = Content(ContentType("text", "x-traceback", {'charset': 'utf8'}), lambda:[_b( "test old mcdonald\n" "failure a\n" "failure: a\n" "error a\n" "error: a\n" "success a\n" "success: a\n" "successful a\n" "successful: a\n" "]\n")]) self.assertEqual(self.client._events, [ ('startTest', self.test), ('addFailure', self.test, details), ('stopTest', self.test), ]) def test_stdout_passthrough(self): """Lines received which cannot be interpreted as any protocol action should be passed through to sys.stdout. """ bytes = _b("randombytes\n") self.protocol.lineReceived(bytes) self.assertEqual(self.stdout.getvalue(), bytes)
def test_communicate_with_input(self): proc = FakeProcess({}, {'stdout': BytesIO(_b('foo'))}) self.assertEqual((_b('foo'), ''), proc.communicate(input=_b("bar")))