Colorize Django Test Output

While practicing Test Driven Development I run tests frequently so it is important for me to see at a glance if they failed or OK. Unfortunately it is not easy with a simple black/white “OK” in between of all the tracing and debugging messages.

Here is a simple patch to fix that annoyance:

  1. By default Django 1.1 uses the system wide unittest module. So first we have to locate the relevant unittest.py. For my Ubuntu Linux 10.04 locate unittest.py returns 7 hits. The relevant one is /usr/lib/python2.6/unittest.py

  2. Now monkey-patching the unittest.py. We will insert some escape sequences, that are understood by every terminal application supporting colors. Search for “OK” (located in the method run), replace with

     "\x1b[0;32;1mTESTED - OK\x1b[0m"
  3. Then enrich “FAILED” with

     "\x1b[0;31;1mTESTS FAILED ("
     ")\x1b[0m"

No we have a colorful TEST FAILED or TESTED OK. Enjoy!

Even better solution

Django supports setting the test runner through the TEST_RUNNER setting. So the “proper” (non-monkey-patching) way would be to

  1. write an own test runner
  2. inherit it from the default test runner
  3. do customization
  4. set TEST_RUNNER in the settings.py to that new class

Comments

blog comments powered by Disqus