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

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
4 participants
@jakebuhler
Contributor

jakebuhler commented Apr 15, 2014

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

@bmispelon

View changes

django/test/runner.py
+ # User does not have Pygments installed, do not highlight
+ return lambda text: text
+
+ if sys.version_info[0] < 3:

This comment has been minimized.

@bmispelon

bmispelon Apr 16, 2014

Member

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

@bmispelon

bmispelon Apr 16, 2014

Member

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

@bmispelon

View changes

django/test/runner.py
+ """
+ A TextTestResult that displays output using ANSI colors.
+
+ The following color pallete options are used to determine the colors

This comment has been minimized.

@bmispelon

bmispelon Apr 16, 2014

Member

Typo: s/pallete/palette/

@bmispelon

bmispelon Apr 16, 2014

Member

Typo: s/pallete/palette/

@bmispelon

View changes

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

This comment has been minimized.

@bmispelon

bmispelon Apr 16, 2014

Member

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

@bmispelon

bmispelon Apr 16, 2014

Member

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

@jakebuhler

This comment has been minimized.

Show comment
Hide comment
@jakebuhler

jakebuhler Apr 17, 2014

Contributor

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
Contributor

jakebuhler commented Apr 17, 2014

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

View changes

django/test/runner.py
+ else:
+ from pygments.lexers import Python3TracebackLexer as Lexer
+
+ return lambda text: highlight(text, Lexer(), TerminalFormatter())

This comment has been minimized.

@charettes

charettes Apr 17, 2014

Member

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

@charettes

charettes Apr 17, 2014

Member

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

This comment has been minimized.

@jakebuhler

jakebuhler Apr 17, 2014

Contributor

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.

@jakebuhler

jakebuhler Apr 17, 2014

Contributor

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.

This comment has been minimized.

@charettes

charettes Apr 17, 2014

Member

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

@charettes

charettes Apr 17, 2014

Member

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

This comment has been minimized.

@jakebuhler

jakebuhler Apr 17, 2014

Contributor

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

@jakebuhler

jakebuhler Apr 17, 2014

Contributor

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

@bmispelon

View changes

docs/ref/django-admin.txt
+.. 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.

This comment has been minimized.

@bmispelon

bmispelon Apr 17, 2014

Member

A link to ANSICON might be useful here.

@bmispelon

bmispelon Apr 17, 2014

Member

A link to ANSICON might be useful here.

@jakebuhler

This comment has been minimized.

Show comment
Hide comment
@jakebuhler

jakebuhler Apr 17, 2014

Contributor

Added links to ANSICON to the docs

Contributor

jakebuhler commented Apr 17, 2014

Added links to ANSICON to the docs

@@ -151,6 +154,142 @@ def run_tests(self, test_labels, extra_tests=None, **kwargs):
return self.suite_result(suite, result)
+class ColorTextTestResult(unittest.TextTestResult):
+

This comment has been minimized.

@timgraham

timgraham Aug 1, 2014

Member

omit newline

@timgraham

timgraham Aug 1, 2014

Member

omit newline

+ self.remove_settings('settings.py')
+
+ @unittest.skipIf(
+ not supports_color() or sys.platform == 'win32' or six.PY3,

This comment has been minimized.

@timgraham

timgraham Aug 1, 2014

Member

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

@timgraham

timgraham Aug 1, 2014

Member

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

@@ -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(

This comment has been minimized.

@timgraham

timgraham Aug 1, 2014

Member

This will need to be updated to use argparse.

@timgraham

timgraham Aug 1, 2014

Member

This will need to be updated to use argparse.

@timgraham

This comment has been minimized.

Show comment
Hide comment
@timgraham

timgraham Nov 4, 2014

Member

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

Member

timgraham commented Nov 4, 2014

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