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
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 7 months ago
Comments
This would be really useful to have in the standard Django testrunner! :-)
#
This is now the standard test runner!
#
as django-discover-runner now comes with django (since 1.6), you should replace
by
#
Please login first before commenting.