Modify ↓
Opened 6 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: |
Description
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 291 291 self.fixture = fixture 292 292 293 293 294 try: 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() 303 except ImportError: 304 pass 305 306 307 class ColoredTextTestResult(unittest.TextTestResult): 308 def __init__(self, *args, **kwargs): 309 unittest.TextTestResult.__init__(self, *args, **kwargs) 310 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) 320 321 322 class ColoredTextTestRunner(unittest.TextTestRunner): 323 def __init__(self, *args, **kwargs): 324 unittest.TextTestRunner.__init__(self, resultclass=ColoredTextTestResult, *args, **kwargs) 325 326 294 327 # -- Database utilities 295 328 296 329 def get_dburi(): -
trac/tests/wikisyntax.py
diff -r 016eaf50cbd9 trac/tests/wikisyntax.py
a b 19 19 from trac.mimeview.api import RenderingContext 20 20 from trac.resource import Resource 21 21 from trac.search.web_ui import SearchModule 22 from trac.test import MockPerm, mkdtemp 22 from trac.test import MockPerm, mkdtemp, ColoredTextTestRunner 23 23 from trac.web.href import Href 24 24 from trac.wiki.tests import formatter 25 25 … … 201 201 return suite 202 202 203 203 if __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)
Note:
See TracTickets
for help on using tickets.