Login

Syntax highlighting for tracebacks in console output

Author:
jezdez
Posted:
August 20, 2012
Language:
Python
Version:
1.4
Tags:
pygments testing test traceback
Score:
1 (after 1 ratings)

This is hardcoded to use django-discover-runner since that's my main test runner but could easily be adopted to use Django's own test runner. If you're using a terminal that is capable of showing 256 colors use the Terminal256Formatter formatter instead.

Enabled it with the TEST_RUNNER setting:

TEST_RUNNER = 'dotted.path.to.highlighted.runner.HighlightedDiscoverRunner'

Where dotted.path.to.highlighted.runner is the Python import path of the file you saved the runner in.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from pygments import highlight
from pygments.lexers import PythonTracebackLexer
from pygments.formatters import TerminalFormatter

from django.utils.unittest import TextTestRunner, TextTestResult

from discover_runner import DiscoverRunner


class HighlightedTextTestResult(TextTestResult):

    def _exc_info_to_string(self, err, test):
        code = super(HighlightedTextTestResult, self)._exc_info_to_string(err, test)
        return highlight(code, PythonTracebackLexer(), TerminalFormatter())


class HighlightedTextTestRunner(TextTestRunner):
    resultclass = HighlightedTextTestResult


class HighlightedDiscoverRunner(DiscoverRunner):

    def run_suite(self, suite, **kwargs):
        return HighlightedTextTestRunner(
            verbosity=self.verbosity, failfast=self.failfast).run(suite)

More like this

  1. Custom nose runner by tomas 6 years, 2 months ago
  2. Class-based coverage test runner by brutasse 5 years, 2 months ago
  3. Test runner that installs 'tests' packages as apps by adrian_lc 1 year, 11 months ago
  4. Deep package test runner by eternicode 4 years, 5 months ago
  5. Test Suite URL Coverage by jterrace 3 years, 6 months ago

Comments

vdboor (on August 20, 2012):

This would be really useful to have in the standard Django testrunner! :-)

#

jezdez (on October 22, 2013):

This is now the standard test runner!

#

JocelynD (on February 10, 2014):

as django-discover-runner now comes with django (since 1.6), you should replace

from discover_runner import DiscoverRunner

by

from django.test.runner import DiscoverRunner

#

Please login first before commenting.