Over a million developers have joined DZone.
Platinum Partner

All Tests Passed - Have a Banana: Colored Output while doing TDD with DJango and Fabric

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix.  Discover how IT departments looking for ways to keep up with demand for business apps has caused a new breed of developers to surface - the Rapid Application Developer.

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


It helps get 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")
        print green_bg("All tests passed - have a banana!")

You can choose your own success and failure messages.

Now we have lovely colors while doing TDD in Django:


Source: http://codeinthehole.com/writing/coloured-output-while-doing-tdd-with-django-and-fabric/

The Web Dev Zone is brought to you in partnership with Mendix.  Learn more about The Essentials of Digital Innovation and how it needs to be at the heart of every organization.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}