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:
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.pyreturns 7 hits. The relevant one is
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"
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
- write an own test runner
- inherit it from the default test runner
- do customization
settings.pyto that new class