Example #1
0
    def _run_core(self):
        # Add an observer to trap all logged errors.
        self.case.reactor = self._reactor
        error_observer = _log_observer
        full_log = StringIO()
        full_observer = log.FileLogObserver(full_log)
        spinner = self._make_spinner()
        successful, unhandled = run_with_log_observers(
            [error_observer.gotEvent, full_observer.emit],
            self._blocking_run_deferred, spinner)

        self.case.addDetail('twisted-log', text_content(full_log.getvalue()))

        logged_errors = error_observer.flushErrors()
        for logged_error in logged_errors:
            successful = False
            self._got_user_failure(logged_error, tb_label='logged-error')

        if unhandled:
            successful = False
            for debug_info in unhandled:
                f = debug_info.failResult
                info = debug_info._getDebugTracebacks()
                if info:
                    self.case.addDetail('unhandled-error-in-deferred-debug',
                                        text_content(info))
                self._got_user_failure(f, 'unhandled-error-in-deferred')

        junk = spinner.clear_junk()
        if junk:
            successful = False
            self._log_user_exception(UncleanReactorError(junk))

        if successful:
            self.result.addSuccess(self.case, details=self.case.getDetails())
Example #2
0
    def test_run_load_list(self):
        self.useFixture(SampleTestFixture())
        out = StringIO()
        # We load two tests - one that exists and one that doesn't, and we
        # should get the one that exists and neither the one that doesn't nor
        # the unmentioned one that does.
        tempdir = self.useFixture(fixtures.TempDir())
        tempname = tempdir.path + '/tests.list'
        f = open(tempname, 'wb')
        try:
            f.write(_b("""
testtools.runexample.TestFoo.test_bar
testtools.runexample.missingtest
"""))
        finally:
            f.close()
        try:
            run.main(['prog', '-l', '--load-list', tempname,
                'testtools.runexample.test_suite'], out)
        except SystemExit:
            exc_info = sys.exc_info()
            raise AssertionError(
                "-l --load-list tried to exit. %r" % exc_info[1])
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
""", out.getvalue())
Example #3
0
    def _run_core(self):
        # Add an observer to trap all logged errors.
        self.case.reactor = self._reactor
        error_observer = _log_observer
        full_log = StringIO()
        full_observer = log.FileLogObserver(full_log)
        spinner = self._make_spinner()
        successful, unhandled = run_with_log_observers(
            [error_observer.gotEvent, full_observer.emit], self._blocking_run_deferred, spinner
        )

        self.case.addDetail("twisted-log", text_content(full_log.getvalue()))

        logged_errors = error_observer.flushErrors()
        for logged_error in logged_errors:
            successful = False
            self._got_user_failure(logged_error, tb_label="logged-error")

        if unhandled:
            successful = False
            for debug_info in unhandled:
                f = debug_info.failResult
                info = debug_info._getDebugTracebacks()
                if info:
                    self.case.addDetail("unhandled-error-in-deferred-debug", text_content(info))
                self._got_user_failure(f, "unhandled-error-in-deferred")

        junk = spinner.clear_junk()
        if junk:
            successful = False
            self._log_user_exception(UncleanReactorError(junk))

        if successful:
            self.result.addSuccess(self.case, details=self.case.getDetails())
Example #4
0
    def test_run_load_list(self):
        if fixtures is None:
            self.skipTest("Need fixtures")
        self.useFixture(SampleTestFixture())
        out = StringIO()
        # We load two tests - one that exists and one that doesn't, and we
        # should get the one that exists and neither the one that doesn't nor
        # the unmentioned one that does.
        tempdir = self.useFixture(fixtures.TempDir())
        tempname = tempdir.path + '/tests.list'
        f = open(tempname, 'wb')
        try:
            f.write(
                _b("""
testtools.runexample.TestFoo.test_bar
testtools.runexample.missingtest
"""))
        finally:
            f.close()
        run.main([
            'prog', '-l', '--load-list', tempname,
            'testtools.runexample.test_suite'
        ], out)
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
""", out.getvalue())
Example #5
0
def run_doctest(obj, name):
    p = doctest.DocTestParser()
    t = p.get_doctest(obj.__doc__, sys.modules[obj.__module__].__dict__, name, "", 0)
    r = doctest.DocTestRunner()
    output = StringIO()
    r.run(t, out=output.write)
    return r.failures, output.getvalue()
Example #6
0
    def test_run_list_failed_import(self):
        broken = self.useFixture(SampleTestFixture(broken=True))
        out = StringIO()
        # XXX: http://bugs.python.org/issue22811
        unittest2.defaultTestLoader._top_level_dir = None
        exc = self.assertRaises(
            SystemExit, run.main,
            ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
        self.assertEqual(2, exc.args[0])
        self.assertThat(
            out.getvalue(),
            DocTestMatches(
                """\
unittest2.loader._FailedTest.runexample
Failed to import test module: runexample
Traceback (most recent call last):
  File ".../loader.py", line ..., in _find_test_path
    package = self._get_module_from_name(name)
  File ".../loader.py", line ..., in _get_module_from_name
    __import__(name)
  File ".../runexample/__init__.py", line 1
    class not in
...^...
SyntaxError: invalid syntax

""", doctest.ELLIPSIS))
Example #7
0
    def test_run_list(self):
        self.useFixture(SampleTestFixture())
        out = StringIO()
        run.main(['prog', '-l', 'testtools.runexample.test_suite'], out)
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
testtools.runexample.TestFoo.test_quux
""", out.getvalue())
Example #8
0
 def _setUp(self):
     logs = StringIO()
     full_observer = log.FileLogObserver(logs)
     self.useFixture(_TwistedLogObservers([full_observer.emit]))
     self.addDetail(
         self.LOG_DETAIL_NAME,
         Content(UTF8_TEXT, lambda: [logs.getvalue().encode("utf-8")]))
Example #9
0
    def test_run_load_list(self):
        self.useFixture(SampleTestFixture())
        out = StringIO()
        # We load two tests - one that exists and one that doesn't, and we
        # should get the one that exists and neither the one that doesn't nor
        # the unmentioned one that does.
        tempdir = self.useFixture(fixtures.TempDir())
        tempname = tempdir.path + '/tests.list'
        f = open(tempname, 'wb')
        try:
            f.write(
                _b("""
testtools.runexample.TestFoo.test_bar
testtools.runexample.missingtest
"""))
        finally:
            f.close()
        try:
            run.main([
                'prog', '-l', '--load-list', tempname,
                'testtools.runexample.test_suite'
            ], out)
        except SystemExit:
            exc_info = sys.exc_info()
            raise AssertionError("-l --load-list tried to exit. %r" %
                                 exc_info[1])
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
""", out.getvalue())
Example #10
0
def run_doctest(obj, name):
    p = doctest.DocTestParser()
    t = p.get_doctest(obj.__doc__, sys.modules[obj.__module__].__dict__, name,
                      '', 0)
    r = doctest.DocTestRunner()
    output = StringIO()
    r.run(t, out=output.write)
    return r.failures, output.getvalue()
Example #11
0
 def test_preserving_existing_handlers(self):
     stream = StringIO()
     self.logger.addHandler(logging.StreamHandler(stream))
     self.logger.setLevel(logging.INFO)
     fixture = LogHandler(self.CustomHandler(), nuke_handlers=False)
     with fixture:
         logging.info("message")
     self.assertEqual(["message"], fixture.handler.msgs)
     self.assertEqual("message\n", stream.getvalue())
Example #12
0
 def test_preserving_existing_handlers(self):
     stream = StringIO()
     self.logger.addHandler(logging.StreamHandler(stream))
     self.logger.setLevel(logging.INFO)
     fixture = FakeLogger(nuke_handlers=False)
     with fixture:
         logging.info("message")
     self.assertEqual("message\n", fixture.output)
     self.assertEqual("message\n", stream.getvalue())
 def _run_external_case(self):
     """Run the prepared test case in a seperate module"""
     sys.path.insert(0, self.dir)
     self.addCleanup(sys.path.remove, self.dir)
     module = __import__(self.modname)
     self.addCleanup(sys.modules.pop, self.modname)
     stream = StringIO()
     self._run(stream, module.Test())
     return stream.getvalue()
Example #14
0
 def test_preserving_existing_handlers(self):
     stream = StringIO()
     self.logger.addHandler(logging.StreamHandler(stream))
     self.logger.setLevel(logging.INFO)
     fixture = LogHandler(self.CustomHandler(), nuke_handlers=False)
     with fixture:
         logging.info("message")
     self.assertEqual(["message"], fixture.handler.msgs)
     self.assertEqual("message\n", stream.getvalue())
Example #15
0
 def test_preserving_existing_handlers(self):
     stream = StringIO()
     self.logger.addHandler(logging.StreamHandler(stream))
     self.logger.setLevel(logging.INFO)
     fixture = FakeLogger(nuke_handlers=False)
     with fixture:
         logging.info("message")
     self.assertEqual("message\n", fixture.output)
     self.assertEqual("message\n", stream.getvalue())
Example #16
0
    def test_run_list(self):
        self.useFixture(SampleTestFixture())
        out = StringIO()
        try:
            run.main(['prog', '-l', 'testtools.runexample.test_suite'], out)
        except SystemExit:
            exc_info = sys.exc_info()
            raise AssertionError("-l tried to exit. %r" % exc_info[1])
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
testtools.runexample.TestFoo.test_quux
""", out.getvalue())
Example #17
0
    def test_run_list_failed_import(self):
        if not run.have_discover:
            self.skipTest("Need discover")
        broken = self.useFixture(SampleTestFixture(broken=True))
        out = StringIO()
        exc = self.assertRaises(
            SystemExit, run.main,
            ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
        self.assertEqual(2, exc.args[0])
        self.assertEqual("""Failed to import
runexample
""", out.getvalue())
Example #18
0
    def test_run_list_failed_import(self):
        if not run.have_discover:
            self.skipTest("Need discover")
        broken = self.useFixture(SampleTestFixture(broken=True))
        out = StringIO()
        exc = self.assertRaises(
            SystemExit,
            run.main, ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
        self.assertEqual(2, exc.args[0])
        self.assertEqual("""Failed to import
runexample
""", out.getvalue())
Example #19
0
 def test_replace_and_restore_handlers(self):
     stream = StringIO()
     logger = logging.getLogger()
     logger.addHandler(logging.StreamHandler(stream))
     logger.setLevel(logging.INFO)
     logging.info("one")
     fixture = FakeLogger()
     with fixture:
         logging.info("two")
     logging.info("three")
     self.assertEqual("two\n", fixture.output)
     self.assertEqual("one\nthree\n", stream.getvalue())
Example #20
0
 def test_replace_and_restore_handlers(self):
     stream = StringIO()
     logger = logging.getLogger()
     logger.addHandler(logging.StreamHandler(stream))
     logger.setLevel(logging.INFO)
     logging.info("one")
     fixture = FakeLogger()
     with fixture:
         logging.info("two")
     logging.info("three")
     self.assertEqual("two\n", fixture.output)
     self.assertEqual("one\nthree\n", stream.getvalue())
Example #21
0
 def test_issue_16662(self):
     # unittest's discover implementation didn't handle load_tests on
     # packages. That is fixed pending commit, but we want to offer it
     # to all testtools users regardless of Python version.
     # See http://bugs.python.org/issue16662
     pkg = self.useFixture(SampleLoadTestsPackage())
     out = StringIO()
     self.assertEqual(None, run.main(
         ['prog', 'discover', '-l', pkg.package.base], out))
     self.assertEqual(dedent("""\
         discoverexample.TestExample.test_foo
         fred
         """), out.getvalue())
Example #22
0
 def test_issue_16662(self):
     # unittest's discover implementation didn't handle load_tests on
     # packages. That is fixed pending commit, but we want to offer it
     # to all testtools users regardless of Python version.
     # See http://bugs.python.org/issue16662
     pkg = self.useFixture(SampleLoadTestsPackage())
     out = StringIO()
     self.assertEqual(
         None, run.main(['prog', 'discover', '-l', pkg.package.base], out))
     self.assertEqual(
         dedent("""\
         discoverexample.TestExample.test_foo
         fred
         """), out.getvalue())
Example #23
0
    def test_stdout_honoured(self):
        self.useFixture(SampleTestFixture())
        tests = []
        out = StringIO()
        exc = self.assertRaises(SystemExit, run.main,
            argv=['prog', 'testtools.runexample.test_suite'],
            stdout=out)
        self.assertEqual((0,), exc.args)
        self.assertThat(
            out.getvalue(),
            MatchesRegex(_u("""Tests running...

Ran 2 tests in \\d.\\d\\d\\ds
OK
""")))
Example #24
0
 def test_just_header_when_no_tests(self):
     stream = StringIO()
     result = subunit.test_results.CsvResult(stream)
     result.startTestRun()
     result.stopTestRun()
     self.assertEqual([['test', 'status', 'start_time', 'stop_time']],
                      self.parse_stream(stream))
Example #25
0
    def test_run_list_with_loader(self):
        # list() is attempted with a loader first.
        self.useFixture(SampleTestFixture())
        tests = []

        class CaptureList(run.TestToolsTestRunner):
            def list(self, test, loader=None):
                tests.append(
                    set([
                        case.id()
                        for case in testtools.testsuite.iterate_tests(test)
                    ]))
                tests.append(loader)

        out = StringIO()
        try:
            program = run.TestProgram(
                argv=['prog', '-l', 'testtools.runexample.test_suite'],
                stdout=out,
                testRunner=CaptureList)
        except SystemExit:
            exc_info = sys.exc_info()
            raise AssertionError("-l tried to exit. %r" % exc_info[1])
        self.assertEqual([
            set([
                'testtools.runexample.TestFoo.test_bar',
                'testtools.runexample.TestFoo.test_quux'
            ]), program.testLoader
        ], tests)
Example #26
0
    def test_run_orders_tests(self):
        self.useFixture(SampleTestFixture())
        out = StringIO()
        # We load two tests - one that exists and one that doesn't, and we
        # should get the one that exists and neither the one that doesn't nor
        # the unmentioned one that does.
        tempdir = self.useFixture(fixtures.TempDir())
        tempname = tempdir.path + '/tests.list'
        f = open(tempname, 'wb')
        try:
            f.write(_b("""
testtools.runexample.TestFoo.test_bar
testtools.runexample.missingtest
"""))
        finally:
            f.close()
        run.main(['prog', '-l', '--load-list', tempname,
            'testtools.runexample.test_suite'], out)
        self.assertEqual("""testtools.runexample.TestFoo.test_bar
""", out.getvalue())
 def test_stopTestRun_count_many(self):
     test = self.make_test()
     self.result.startTestRun()
     self.result.startTest(test)
     self.result.stopTest(test)
     self.result.startTest(test)
     self.result.stopTest(test)
     self.result.stream = StringIO()
     self.result.stopTestRun()
     self.assertThat(self.getvalue(),
         DocTestMatches("Ran 2 tests in ...s\n...", doctest.ELLIPSIS))
Example #28
0
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())
Example #29
0
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())
Example #30
0
    def test_run_list_failed_import(self):
        broken = self.useFixture(SampleTestFixture(broken=True))
        out = StringIO()
        # XXX: http://bugs.python.org/issue22811
        unittest2.defaultTestLoader._top_level_dir = None
        exc = self.assertRaises(
            SystemExit,
            run.main, ['prog', 'discover', '-l', broken.package.base, '*.py'], out)
        self.assertEqual(2, exc.args[0])
        self.assertThat(out.getvalue(), DocTestMatches("""\
Failed to import test module: runexample
Traceback (most recent call last):
  File ".../loader.py", line ..., in _find_test_path
    package = self._get_module_from_name(name)
  File ".../loader.py", line ..., in _get_module_from_name
    __import__(name)
  File ".../runexample/__init__.py", line 1
    class not in
...^...
SyntaxError: invalid syntax

""", doctest.ELLIPSIS))
Example #31
0
 def test_csv_output(self):
     stream = StringIO()
     result = subunit.test_results.CsvResult(stream)
     if sys.version_info >= (3, 0):
         result._now = iter(range(5)).__next__
     else:
         result._now = iter(range(5)).next
     result.startTestRun()
     result.startTest(self)
     result.addSuccess(self)
     result.stopTest(self)
     result.stopTestRun()
     self.assertEqual([
         ['test', 'status', 'start_time', 'stop_time'],
         [self.id(), 'success', '0', '1'],
     ], self.parse_stream(stream))
Example #32
0
 def setUp(self):
     self.output = StringIO()
     self.result = subunit.TestResultStats(self.output)
     self.input_stream = BytesIO()
     self.test = subunit.ProtocolTestCase(self.input_stream)
Example #33
0
 def test_from_stream(self):
     data = StringIO('some data')
     content = content_from_stream(data, UTF8_TEXT, chunk_size=2)
     self.assertThat(list(content.iter_bytes()),
                     Equals(['so', 'me', ' d', 'at', 'a']))
Example #34
0
 def test_no_output_before_events(self):
     stream = StringIO()
     subunit.test_results.CsvResult(stream)
     self.assertEqual([], self.parse_stream(stream))
 def setUp(self):
     super(TestTextTestResult, self).setUp()
     self.result = TextTestResult(StringIO())
 def makeResult(self):
     return TextTestResult(StringIO())
Example #37
0
 def test_from_stream_default_type(self):
     data = StringIO('some data')
     content = content_from_stream(data)
     self.assertThat(content.content_type, Equals(UTF8_TEXT))
Example #38
0
class TestSubUnitTags(unittest.TestCase):
    def setUp(self):
        self.original = StringIO()
        self.filtered = StringIO()

    def test_add_tag(self):
        self.original.write("tags: foo\n")
        self.original.write("test: test\n")
        self.original.write("tags: bar -quux\n")
        self.original.write("success: test\n")
        self.original.seek(0)
        result = subunit.tag_stream(self.original, self.filtered, ["quux"])
        self.assertEqual(
            ["tags: quux", "tags: foo", "test: test", "tags: bar", "success: test"],
            self.filtered.getvalue().splitlines(),
        )

    def test_remove_tag(self):
        self.original.write("tags: foo\n")
        self.original.write("test: test\n")
        self.original.write("tags: bar -quux\n")
        self.original.write("success: test\n")
        self.original.seek(0)
        result = subunit.tag_stream(self.original, self.filtered, ["-bar"])
        self.assertEqual(
            ["tags: -bar", "tags: foo", "test: test", "tags: -quux", "success: test"],
            self.filtered.getvalue().splitlines(),
        )
Example #39
0
 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):
     self.tap = StringIO()
     self.subunit = StringIO()
 def setUp(self):
     self.original = StringIO()
     self.filtered = StringIO()
class TestSubUnitTags(unittest.TestCase):
    def setUp(self):
        self.original = StringIO()
        self.filtered = StringIO()

    def test_add_tag(self):
        self.original.write("tags: foo\n")
        self.original.write("test: test\n")
        self.original.write("tags: bar -quux\n")
        self.original.write("success: test\n")
        self.original.seek(0)
        result = subunit.tag_stream(self.original, self.filtered, ["quux"])
        self.assertEqual([
            "tags: quux",
            "tags: foo",
            "test: test",
            "tags: bar",
            "success: test",
        ],
                         self.filtered.getvalue().splitlines())

    def test_remove_tag(self):
        self.original.write("tags: foo\n")
        self.original.write("test: test\n")
        self.original.write("tags: bar -quux\n")
        self.original.write("success: test\n")
        self.original.seek(0)
        result = subunit.tag_stream(self.original, self.filtered, ["-bar"])
        self.assertEqual([
            "tags: -bar",
            "tags: foo",
            "test: test",
            "tags: -quux",
            "success: test",
        ],
                         self.filtered.getvalue().splitlines())
Example #43
0
 def setUp(self):
     self.original = StringIO()
     self.filtered = StringIO()
Example #44
0
 def _setUp(self):
     logs = StringIO()
     full_observer = log.FileLogObserver(logs)
     self.useFixture(_TwistedLogObservers([full_observer.emit]))
     self.addDetail(self.LOG_DETAIL_NAME,
                    Content(UTF8_TEXT, lambda: [logs.getvalue()]))
Example #45
0
class TestTAP2SubUnit(unittest.TestCase):
    """Tests for TAP2SubUnit.

    These tests test TAP string data in, and subunit string data out.
    This is ok because the subunit protocol is intended to be stable,
    but it might be easier/pithier to write tests against TAP string in,
    parsed subunit objects out (by hooking the subunit stream to a subunit
    protocol server.
    """

    def setUp(self):
        self.tap = StringIO()
        self.subunit = StringIO()

    def test_skip_entire_file(self):
        # A file
        # 1..- # Skipped: comment
        # results in a single skipped test.
        self.tap.write("1..0 # Skipped: entire file skipped\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test file skip",
            "skip file skip [",
            "Skipped: entire file skipped",
            "]",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_test_pass(self):
        # A file
        # ok
        # results in a passed test with name 'test 1' (a synthetic name as tap
        # does not require named fixtures - it is the first test in the tap
        # stream).
        self.tap.write("ok\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "success test 1",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_test_number_pass(self):
        # A file
        # ok 1
        # results in a passed test with name 'test 1'
        self.tap.write("ok 1\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "success test 1",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_test_number_description_pass(self):
        # A file
        # ok 1 - There is a description
        # results in a passed test with name 'test 1 - There is a description'
        self.tap.write("ok 1 - There is a description\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1 - There is a description",
            "success test 1 - There is a description",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_test_description_pass(self):
        # A file
        # ok There is a description
        # results in a passed test with name 'test 1 There is a description'
        self.tap.write("ok There is a description\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1 There is a description",
            "success test 1 There is a description",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_SKIP_skip(self):
        # A file
        # ok # SKIP
        # results in a skkip test with name 'test 1'
        self.tap.write("ok # SKIP\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "skip test 1",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_skip_number_comment_lowercase(self):
        self.tap.write("ok 1 # skip no samba environment available, skipping compilation\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "skip test 1 [", 
            "no samba environment available, skipping compilation",
            "]"
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_number_description_SKIP_skip_comment(self):
        # A file
        # ok 1 foo  # SKIP Not done yet
        # results in a skip test with name 'test 1 foo' and a log of
        # Not done yet
        self.tap.write("ok 1 foo  # SKIP Not done yet\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1 foo",
            "skip test 1 foo [",
            "Not done yet",
            "]",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_SKIP_skip_comment(self):
        # A file
        # ok # SKIP Not done yet
        # results in a skip test with name 'test 1' and a log of Not done yet
        self.tap.write("ok # SKIP Not done yet\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "skip test 1 [",
            "Not done yet",
            "]",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_TODO_xfail(self):
        # A file
        # ok # TODO
        # results in a xfail test with name 'test 1'
        self.tap.write("ok # TODO\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "xfail test 1",
            ],
            self.subunit.getvalue().splitlines())

    def test_ok_TODO_xfail_comment(self):
        # A file
        # ok # TODO Not done yet
        # results in a xfail test with name 'test 1' and a log of Not done yet
        self.tap.write("ok # TODO Not done yet\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1",
            "xfail test 1 [",
            "Not done yet",
            "]",
            ],
            self.subunit.getvalue().splitlines())

    def test_bail_out_errors(self):
        # A file with line in it
        # Bail out! COMMENT
        # is treated as an error
        self.tap.write("ok 1 foo\n")
        self.tap.write("Bail out! Lifejacket engaged\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            "test test 1 foo",
            "success test 1 foo",
            "test Bail out! Lifejacket engaged",
            "error Bail out! Lifejacket engaged",
            ],
            self.subunit.getvalue().splitlines())

    def test_missing_test_at_end_with_plan_adds_error(self):
        # A file
        # 1..3
        # ok first test
        # not ok third test
        # results in three tests, with the third being created
        self.tap.write('1..3\n')
        self.tap.write('ok first test\n')
        self.tap.write('not ok second test\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 first test',
            'success test 1 first test',
            'test test 2 second test',
            'failure test 2 second test',
            'test test 3',
            'error test 3 [',
            'test missing from TAP output',
            ']',
            ],
            self.subunit.getvalue().splitlines())

    def test_missing_test_with_plan_adds_error(self):
        # A file
        # 1..3
        # ok first test
        # not ok 3 third test
        # results in three tests, with the second being created
        self.tap.write('1..3\n')
        self.tap.write('ok first test\n')
        self.tap.write('not ok 3 third test\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 first test',
            'success test 1 first test',
            'test test 2',
            'error test 2 [',
            'test missing from TAP output',
            ']',
            'test test 3 third test',
            'failure test 3 third test',
            ],
            self.subunit.getvalue().splitlines())

    def test_missing_test_no_plan_adds_error(self):
        # A file
        # ok first test
        # not ok 3 third test
        # results in three tests, with the second being created
        self.tap.write('ok first test\n')
        self.tap.write('not ok 3 third test\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 first test',
            'success test 1 first test',
            'test test 2',
            'error test 2 [',
            'test missing from TAP output',
            ']',
            'test test 3 third test',
            'failure test 3 third test',
            ],
            self.subunit.getvalue().splitlines())

    def test_four_tests_in_a_row_trailing_plan(self):
        # A file
        # ok 1 - first test in a script with no plan at all
        # not ok 2 - second
        # ok 3 - third
        # not ok 4 - fourth
        # 1..4
        # results in four tests numbered and named
        self.tap.write('ok 1 - first test in a script with trailing plan\n')
        self.tap.write('not ok 2 - second\n')
        self.tap.write('ok 3 - third\n')
        self.tap.write('not ok 4 - fourth\n')
        self.tap.write('1..4\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 - first test in a script with trailing plan',
            'success test 1 - first test in a script with trailing plan',
            'test test 2 - second',
            'failure test 2 - second',
            'test test 3 - third',
            'success test 3 - third',
            'test test 4 - fourth',
            'failure test 4 - fourth'
            ],
            self.subunit.getvalue().splitlines())

    def test_four_tests_in_a_row_with_plan(self):
        # A file
        # 1..4
        # ok 1 - first test in a script with no plan at all
        # not ok 2 - second
        # ok 3 - third
        # not ok 4 - fourth
        # results in four tests numbered and named
        self.tap.write('1..4\n')
        self.tap.write('ok 1 - first test in a script with a plan\n')
        self.tap.write('not ok 2 - second\n')
        self.tap.write('ok 3 - third\n')
        self.tap.write('not ok 4 - fourth\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 - first test in a script with a plan',
            'success test 1 - first test in a script with a plan',
            'test test 2 - second',
            'failure test 2 - second',
            'test test 3 - third',
            'success test 3 - third',
            'test test 4 - fourth',
            'failure test 4 - fourth'
            ],
            self.subunit.getvalue().splitlines())

    def test_four_tests_in_a_row_no_plan(self):
        # A file
        # ok 1 - first test in a script with no plan at all
        # not ok 2 - second
        # ok 3 - third
        # not ok 4 - fourth
        # results in four tests numbered and named
        self.tap.write('ok 1 - first test in a script with no plan at all\n')
        self.tap.write('not ok 2 - second\n')
        self.tap.write('ok 3 - third\n')
        self.tap.write('not ok 4 - fourth\n')
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 - first test in a script with no plan at all',
            'success test 1 - first test in a script with no plan at all',
            'test test 2 - second',
            'failure test 2 - second',
            'test test 3 - third',
            'success test 3 - third',
            'test test 4 - fourth',
            'failure test 4 - fourth'
            ],
            self.subunit.getvalue().splitlines())

    def test_todo_and_skip(self):
        # A file
        # not ok 1 - a fail but # TODO but is TODO
        # not ok 2 - another fail # SKIP instead
        # results in two tests, numbered and commented.
        self.tap.write("not ok 1 - a fail but # TODO but is TODO\n")
        self.tap.write("not ok 2 - another fail # SKIP instead\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1 - a fail but',
            'xfail test 1 - a fail but [',
            'but is TODO',
            ']',
            'test test 2 - another fail',
            'skip test 2 - another fail [',
            'instead',
            ']',
            ],
            self.subunit.getvalue().splitlines())

    def test_leading_comments_add_to_next_test_log(self):
        # A file
        # # comment
        # ok 
        # ok
        # results in a single test with the comment included
        # in the first test and not the second.
        self.tap.write("# comment\n")
        self.tap.write("ok\n")
        self.tap.write("ok\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1',
            'success test 1 [',
            '# comment',
            ']',
            'test test 2',
            'success test 2',
            ],
            self.subunit.getvalue().splitlines())
    
    def test_trailing_comments_are_included_in_last_test_log(self):
        # A file
        # ok foo
        # ok foo
        # # comment
        # results in a two tests, with the second having the comment
        # attached to its log.
        self.tap.write("ok\n")
        self.tap.write("ok\n")
        self.tap.write("# comment\n")
        self.tap.seek(0)
        result = subunit.TAP2SubUnit(self.tap, self.subunit)
        self.assertEqual(0, result)
        self.assertEqual([
            'test test 1',
            'success test 1',
            'test test 2',
            'success test 2 [',
            '# comment',
            ']',
            ],
            self.subunit.getvalue().splitlines())
 def reset_output(self):
     self.result.stream = StringIO()