Syntax highlighting for tracebacks during tests

 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 4 years, 10 months ago
  2. Class-based coverage test runner by brutasse 3 years, 10 months ago
  3. Test runner that installs 'tests' packages as apps by adrian_lc 6 months, 3 weeks ago
  4. Deep package test runner by eternicode 3 years, 1 month ago
  5. Test Suite URL Coverage by jterrace 2 years, 1 month 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

#

(Forgotten your password?)