def test_log_err_is_error(self):
        # An error logged during the test run is recorded as an error in the
        # tests.
        class LogAnError(TestCase):
            def test_something(self):
                try:
                    1 / 0
                except ZeroDivisionError:
                    f = failure.Failure()
                log.err(f)

        test = LogAnError('test_something')
        runner = self.make_runner(test, store_twisted_logs=False)
        result = self.make_result()
        runner.run(result)
        self.assertThat(
            result._events,
            MatchesEvents(('startTest', test), ('addError', test, {
                'logged-error':
                AsText(
                    ContainsAll([
                        'Traceback (most recent call last):',
                        'ZeroDivisionError',
                    ])),
            }), ('stopTest', test)))
    def test_captures_logs(self):
        # CaptureTwistedLogs stores all Twisted log messages as a detail.
        from testtools.twistedsupport import CaptureTwistedLogs

        class SomeTest(TestCase):
            def test_something(self):
                self.useFixture(CaptureTwistedLogs())
                log.msg('foo')

        test = SomeTest('test_something')
        test.run()
        self.assertThat(
            test.getDetails(),
            MatchesDict({
                'twisted-log': AsText(Contains('foo')),
            }))
    def test_log_in_details(self):
        # If store_twisted_logs is True, we include the Twisted logs as
        # details.
        class LogAnError(TestCase):
            def test_something(self):
                log.msg("foo")
                1 / 0

        test = LogAnError('test_something')
        runner = self.make_runner(test, store_twisted_logs=True)
        result = self.make_result()
        runner.run(result)
        self.assertThat(
            result._events,
            MatchesEvents(('startTest', test),
                          ('addError', test, {
                              'traceback': Not(Is(None)),
                              'twisted-log': AsText(EndsWith(' foo\n')),
                          }), ('stopTest', test)))