Beispiel #1
0
    def _printTraceback(self, test, err):
        """Print a nicely formatted traceback.

        :arg err: exc_info()-style traceback triple
        :arg test: the test that precipitated this call

        """
        # Don't bind third item to a local var; that can create
        # circular refs which are expensive to collect. See the
        # sys.exc_info() docs.
        exception_type, exception_value = err[:2]
        extracted_tb = extract_relevant_tb(
            err[2], exception_type, exception_type is test.failureException)
        test_frame_index = index_of_test_frame(extracted_tb, exception_type,
                                               exception_value, test)
        if test_frame_index:
            # We have a good guess at which frame is the test, so
            # trim everything until that. We don't care to see test
            # framework frames.
            extracted_tb = extracted_tb[test_frame_index:]

        with self.bar.dodging():
            self.stream.write(''.join(
                format_traceback(extracted_tb, exception_type, exception_value,
                                 self._cwd, self._term,
                                 self._options.function_color,
                                 self._options.dim_color,
                                 self._options.editor)))
Beispiel #2
0
    def _printError(self, kind, err, test, isFailure=True):
        """Output a human-readable error report to the stream.

        kind -- the (string) type of incident the precipitated this call
        err -- exc_info()-style traceback triple
        test -- the test that precipitated this call

        """
        if isFailure or self._options.show_advisories:
            writeln = self.stream.writeln
            write = self.stream.write
            with self.bar.dodging():
                writeln('\n' +
                        (self._term.bold if isFailure else '') +
                        '%s: %s' % (kind, nose_selector(test)) +
                        (self._term.normal if isFailure else ''))  # end bold

                if isFailure:  # Then show traceback
                    # Don't bind third item to a local var; that can create
                    # circular refs which are expensive to collect. See the
                    # sys.exc_info() docs.
                    exception_type, exception_value = err[:2]
                    extracted_tb = extract_relevant_tb(
                        err[2],
                        exception_type,
                        exception_type is test.failureException)
                    test_frame_index = index_of_test_frame(
                        extracted_tb,
                        exception_type,
                        exception_value,
                        test)
                    if test_frame_index:
                        # We have a good guess at which frame is the test, so
                        # trim everything until that. We don't care to see test
                        # framework frames.
                        extracted_tb = extracted_tb[test_frame_index:]

                    write(''.join(
                        format_traceback(
                            extracted_tb,
                            exception_type,
                            exception_value,
                            self._cwd,
                            self._term,
                            self._options.function_color,
                            self._options.dim_color,
                            self._options.editor)))
Beispiel #3
0
    def _printTraceback(self, test, err):
        """Print a nicely formatted traceback.

        :arg err: exc_info()-style traceback triple
        :arg test: the test that precipitated this call

        """
        # Don't bind third item to a local var; that can create
        # circular refs which are expensive to collect. See the
        # sys.exc_info() docs.
        exception_type, exception_value = err[:2]
        # TODO: In Python 3, the traceback is attached to the exception
        # instance through the __traceback__ attribute. If the instance
        # is saved in a local variable that persists outside the except
        # block, the traceback will create a reference cycle with the
        # current frame and its dictionary of local variables. This will
        # delay reclaiming dead resources until the next cyclic garbage
        # collection pass.

        extracted_tb = extract_relevant_tb(
            err[2],
            exception_type,
            exception_type is test.failureException)
        test_frame_index = index_of_test_frame(
            extracted_tb,
            exception_type,
            exception_value,
            test)
        if test_frame_index:
            # We have a good guess at which frame is the test, so
            # trim everything until that. We don't care to see test
            # framework frames.
            extracted_tb = extracted_tb[test_frame_index:]

        with self.bar.dodging():
            self.stream.write(''.join(
                format_traceback(
                    extracted_tb,
                    exception_type,
                    exception_value,
                    self._cwd,
                    self._term,
                    self._options.function_color,
                    self._options.dim_color,
                    self._options.editor,
                    self._options.editor_shortcut_template)))
def test_index_when_syntax_error_in_test_frame():
    """Make sure ``index_of_test_frame()`` returns None for SyntaxErrors in the test frame.

    When the SyntaxError is in the test frame, the test frame doesn't show up
    in the traceback. We reproduce this below by not referencing.

    """
    extracted_tb = \
        [('/nose/loader.py', 379, 'loadTestsFromName', 'addr.filename, addr.module)'),
         ('/nose/importer.py', 39, 'importFromPath', 'return self.importFromDir(dir_path, fqname)'),
         ('/nose/importer.py', 86, 'importFromDir', 'mod = load_module(part_fqname, fh, filename, desc)')]
    eq_(index_of_test_frame(extracted_tb,
                            SyntaxError,
                            SyntaxError('invalid syntax',
                                        ('tests.py', 120, 1, "{'fields': ['id'],\n")),
                            dummy_test),
        None)
Beispiel #5
0
def test_index_when_syntax_error_in_test_frame():
    """Make sure ``index_of_test_frame()`` returns None for SyntaxErrors in the test frame.

    When the SyntaxError is in the test frame, the test frame doesn't show up
    in the traceback. We reproduce this below by not referencing.

    """
    extracted_tb = \
        [('/nose/loader.py', 379, 'loadTestsFromName', 'addr.filename, addr.module)'),
         ('/nose/importer.py', 39, 'importFromPath', 'return self.importFromDir(dir_path, fqname)'),
         ('/nose/importer.py', 86, 'importFromDir', 'mod = load_module(part_fqname, fh, filename, desc)')]
    eq_(
        index_of_test_frame(
            extracted_tb, SyntaxError,
            SyntaxError('invalid syntax',
                        ('tests.py', 120, 1, "{'fields': ['id'],\n")),
            dummy_test), None)
def test_index_when_syntax_error_below_test_frame():
    """Make sure we manage to find the test frame if there's a SyntaxError below it.

    Here we present to ``index_of_test_frame()`` a traceback that represents
    this test raising a SyntaxError indirectly, in a function called by same
    test.

    """
    extracted_tb = [('/nose/case.py', 183, 'runTest', 'self.test(*self.arg)'),
                    # Legit path so the frame finder can compare to the address of DummyCase:
                    (src(realpath(__file__)), 34, 'test_index_when_syntax_error_below_test_frame', 'deeper()'),
                    ('/noseprogressive/tests/test_utils.py', 33, 'deeper', 'import noseprogressive.tests.syntaxerror')]
    eq_(index_of_test_frame(extracted_tb,
                            SyntaxError,
                            SyntaxError('invalid syntax',
                                        ('/tests/syntaxerror.py', 1, 1, ':bad\n')),
                            dummy_test),
        1)
Beispiel #7
0
def test_index_when_syntax_error_below_test_frame():
    """Make sure we manage to find the test frame if there's a SyntaxError below it.

    Here we present to ``index_of_test_frame()`` a traceback that represents
    this test raising a SyntaxError indirectly, in a function called by same
    test.

    """
    extracted_tb = [
        ('/nose/case.py', 183, 'runTest', 'self.test(*self.arg)'),
        # Legit path so the frame finder can compare to the address of DummyCase:
        (src(realpath(__file__)), 34,
         'test_index_when_syntax_error_below_test_frame', 'deeper()'),
        ('/noseprogressive/tests/test_utils.py', 33, 'deeper',
         'import noseprogressive.tests.syntaxerror')
    ]
    eq_(
        index_of_test_frame(
            extracted_tb, SyntaxError,
            SyntaxError('invalid syntax',
                        ('/tests/syntaxerror.py', 1, 1, ':bad\n')),
            dummy_test), 1)
Beispiel #8
0
    def _printTraceback(self, test, err):
        """Print a nicely formatted traceback.

        :arg err: exc_info()-style traceback triple
        :arg test: the test that precipitated this call

        """
        # Don't bind third item to a local var; that can create
        # circular refs which are expensive to collect. See the
        # sys.exc_info() docs.
        exception_type, exception_value = err[:2]
        extracted_tb = extract_relevant_tb(
            err[2],
            exception_type,
            exception_type is test.failureException)
        test_frame_index = index_of_test_frame(
            extracted_tb,
            exception_type,
            exception_value,
            test)
        if test_frame_index:
            # We have a good guess at which frame is the test, so
            # trim everything until that. We don't care to see test
            # framework frames.
            extracted_tb = extracted_tb[test_frame_index:]

        with self.bar.dodging():
            self.stream.write(''.join(
                format_traceback(
                    extracted_tb,
                    exception_type,
                    exception_value,
                    self._cwd,
                    self._term,
                    self._options.function_color,
                    self._options.dim_color,
                    self._options.editor)))