Login

Syntax highlighting for tracebacks in console output

Author:
jezdez
Posted:
August 20, 2012
Language:
Python
Version:
1.4
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 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.