Edgewall Software

Opened 4 years ago

#13033 new enhancement

Colored test output

Reported by: anonymous Owned by:
Priority: normal Milestone:
Component: general Version:
Severity: normal Keywords: tests pygments colorama
Cc: Branch:
Release Notes:
API Changes:
Internal Changes:


Unit test failures are nicer to read when the diff is syntax highlighted.

Very primitive hack:

  • trac/test.py

    diff -r 016eaf50cbd9 trac/test.py
    a b  
    291291        self.fixture = fixture
     295    import pygments
     296    import pygments.lexers
     297    import pygments.formatters
     298    import colorama
     299    colorama.init()
     300    hascolorhighlighting = True
     301    formatter = pygments.formatters.TerminalFormatter(style='trac')
     302    lexer = pygments.lexers.DiffLexer()
     303except ImportError:
     304    pass
     307class ColoredTextTestResult(unittest.TextTestResult):
     308    def __init__(self, *args, **kwargs):
     309        unittest.TextTestResult.__init__(self, *args, **kwargs)
     311    def printErrorList(self, flavour, errors):
     312        for test, err in errors:
     313            self.stream.writeln(self.separator1)
     314            self.stream.writeln("%s: %s" % (flavour,self.getDescription(test)))
     315            self.stream.writeln(self.separator2)
     316            if hascolorhighlighting:
     317                print("%s" % pygments.highlight(err, lexer, formatter))
     318            else:
     319                self.stream.writeln("%s" % err)
     322class ColoredTextTestRunner(unittest.TextTestRunner):
     323    def __init__(self, *args, **kwargs):
     324        unittest.TextTestRunner.__init__(self, resultclass=ColoredTextTestResult, *args, **kwargs)
    294327# -- Database utilities
    296329def get_dburi():
  • trac/tests/wikisyntax.py

    diff -r 016eaf50cbd9 trac/tests/wikisyntax.py
    a b  
    1919from trac.mimeview.api import RenderingContext
    2020from trac.resource import Resource
    2121from trac.search.web_ui import SearchModule
    22 from trac.test import MockPerm, mkdtemp
     22from trac.test import MockPerm, mkdtemp, ColoredTextTestRunner
    2323from trac.web.href import Href
    2424from trac.wiki.tests import formatter
    201201    return suite
    203203if __name__ == '__main__':
    204     unittest.main(defaultTest='test_suite')
     204    unittest.main(defaultTest='test_suite', testRunner=ColoredTextTestRunner)

All other unittest.main also have to be updated.

A new dependency colorama is optionally required for this.

Using print instead of using stream is probably bad.

Always using DiffLexer instead of custom lexer depending on error is probably bad.

Always using TerminalFormatter instead of limited color formatter is probably bad.

Maybe this is too much effort to really do nicely. But maybe you like colors.

Attachments (0)

Change History (0)

Modify Ticket

Change Properties
Set your email in Preferences
as new The ticket will remain with no owner.
The ticket will be disowned.
as The resolution will be set. Next status will be 'closed'.
The owner will be changed from (none) to anonymous. Next status will be 'assigned'.

Add Comment

E-mail address and name can be saved in the Preferences .
Note: See TracTickets for help on using tickets.