Coloured output while doing TDD with Django and Fabric

I’m a big fan of using PHPUnit with console colours turned on (using the --colors option). Eg:

image

It helps gets into the natural “red, green, refactor” rhythm.

I’m currently totally immersed in Django, and greatly miss the lack of colour support within the “test” management command. A simple workaround for this is to use Fabric with a few modified color commands. Your fabric file should include the following:

from fabric.colors import _wrap_with

green_bg = _wrap_with('42')
red_bg = _wrap_with('41')

# Set the list of apps to test
env.test_apps = "app1 app2"

def test():
    with settings(warn_only=True):
        result = local('./manage.py test %(test_apps)s --settings=settings_test -v 2 --failfast' % env, capture=False)
    if result.failed:
        print red_bg("Some tests failed")
    else:
        print
        print green_bg("All tests passed - have a banana!")

You can choose your own success and failure messages.

Now we have lovely colours while doing TDD in Django:

image

----

Tagged with: python, testing
Filed in: tips
Revision history

Previous: Domain-model-mapper - A PHP Data Mapper implementation
Next: Multi-scp: copying a file to all hosts in your SSH config

Copyright © 2005-2017 David Winterbottom
Content licensed under CC BY-NC-SA 4.0.