Skip to content

Fixed #22449 -- Colorized output of test results #2565

Closed
wants to merge 1 commit into from

4 participants

@jakebuhler

Added tests, code, and documentation for colorizing the output of the manage.py test commmand.

@bmispelon bmispelon commented on an outdated diff Apr 16, 2014
django/test/runner.py
+ """
+ Return a function for highlighting traceback text.
+
+ If the Pygments library is available, it will be used to create
+ a function that takes traceback text and returns an ANSI-highlighted
+ version. If Pygments is not available, the returned highlighter will
+ simply return the traceback text unchanged.
+ """
+ try:
+ from pygments import highlight
+ from pygments.formatters import TerminalFormatter
+ except ImportError:
+ # User does not have Pygments installed, do not highlight
+ return lambda text: text
+
+ if sys.version_info[0] < 3:
@bmispelon
Django member
bmispelon added a note Apr 16, 2014

You can use if six.PY2 (import six from django.utils)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bmispelon bmispelon commented on an outdated diff Apr 16, 2014
django/test/runner.py
@@ -151,6 +154,138 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
return self.suite_result(suite, result)
+class ColorTextTestResult(unittest.TextTestResult):
+
+ """
+ A TextTestResult that displays output using ANSI colors.
+
+ The following color pallete options are used to determine the colors
@bmispelon
Django member
bmispelon added a note Apr 16, 2014

Typo: s/pallete/palette/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bmispelon bmispelon commented on an outdated diff Apr 16, 2014
tests/runtests.py
@@ -204,6 +205,10 @@ def django_tests(verbosity, interactive, failfast, test_labels):
interactive=interactive,
failfast=failfast,
)
+
+ # Colorize output
+ test_runner.test_runner.resultclass = ColorTextTestResult
@bmispelon
Django member
bmispelon added a note Apr 16, 2014

There should be a way for users to disable the coloring of runtests too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jakebuhler

Hi Baptiste, Thanks for the good suggestions. I have implemented them:

  • Fixed misspelling of "palette"
  • Added --no-color option to runtests.py
  • Used six.PY2 to check the current Python version

I also added the following:

  • Clarification in the release notes and admin command docs that ANSICON is required for color output on Windows
  • A fix to suppress traceback highlighting when Pygments is installed but the terminal does not support color
@charettes charettes and 1 other commented on an outdated diff Apr 17, 2014
django/test/runner.py
+ if not supports_color():
+ return lambda text: text
+
+ try:
+ from pygments import highlight
+ from pygments.formatters import TerminalFormatter
+ except ImportError:
+ # User does not have Pygments installed, do not highlight
+ return lambda text: text
+
+ if six.PY2:
+ from pygments.lexers import PythonTracebackLexer as Lexer
+ else:
+ from pygments.lexers import Python3TracebackLexer as Lexer
+
+ return lambda text: highlight(text, Lexer(), TerminalFormatter())
@charettes
Django member
charettes added a note Apr 17, 2014

Something is wrong with the indentation here, you might want to use flake8 from the top directory to spot warnings.

@jakebuhler
jakebuhler added a note Apr 17, 2014

When I run this through flake8 I'm not getting any errors. Also, this code is running fine for me. I'm guessing the problem might have to do with the fact that I checked this in from a Windows machine :\ I'll check out the changes on my Macbook when I get home in a few hours.

@charettes
Django member
charettes added a note Apr 17, 2014

Oh the code looks fine, it's just that this should be 4-space indented not 8.

@jakebuhler
jakebuhler added a note Apr 17, 2014

Ah, I see, sorry about that :) This is now fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@bmispelon bmispelon commented on an outdated diff Apr 17, 2014
docs/ref/django-admin.txt
@@ -1275,6 +1275,11 @@ The ``--liveserver`` option can be used to override the default address where
the live server (used with :class:`~django.test.LiveServerTestCase`) is
expected to run from. The default value is ``localhost:8081``.
+.. versionadded:: 1.8
+
+If the Pygments library is available, the tracebacks of failing tests will be
+colored. Windows users will need to have ANSICON installed for this to work.
@bmispelon
Django member
bmispelon added a note Apr 17, 2014

A link to ANSICON might be useful here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jakebuhler

Added links to ANSICON to the docs

@timgraham timgraham commented on the diff Aug 1, 2014
django/test/runner.py
@@ -151,6 +154,142 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
return self.suite_result(suite, result)
+class ColorTextTestResult(unittest.TextTestResult):
+
@timgraham
Django member
timgraham added a note Aug 1, 2014

omit newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Aug 1, 2014
tests/admin_scripts/tests.py
class ManageTestCommand(AdminScriptTestCase):
def setUp(self):
from django.core.management.commands.test import Command as TestCommand
self.cmd = TestCommand()
+ self.write_settings('settings.py', sdict={
+ 'RUN_FROM_MANAGE_TEST_COMMAND': True,
+ })
+
+ def tearDown(self):
+ self.remove_settings('settings.py')
+
+ @unittest.skipIf(
+ not supports_color() or sys.platform == 'win32' or six.PY3,
@timgraham
Django member
timgraham added a note Aug 1, 2014

isn't the win32 check in supports_color()? do the tests not work with ANSICON?
why do we skip on Python 3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham timgraham commented on the diff Aug 1, 2014
tests/runtests.py
@@ -351,6 +357,10 @@ def paired_tests(paired_test, options, test_labels):
'--selenium', action='store_true', dest='selenium',
default=False,
help='Run the Selenium tests as well (if Selenium is installed)')
+ parser.add_option(
@timgraham
Django member
timgraham added a note Aug 1, 2014

This will need to be updated to use argparse.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@timgraham
Django member

Closing in absence of follow-up. Please send a new PR if you can update it. Thanks!

@timgraham timgraham closed this Nov 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.