Python Zone is brought to you in partnership with:

I work at Tangent Labs, a digital agency, writing applications in python and django. I spend most of my free time hacking. I run commandlinefu.com and am the author of the e-commerce framework django-oscar amongst other things. I used to be a mathematician; I have a PhD in Mathematics from the University of Nottingham and an associated interest in cryptic crosswords, chess and playing devil's advocate. David is a DZone MVB and is not an employee of DZone and has posted 27 posts at DZone. You can read more from them at their website. View Full User Profile

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

02.07.2012
| 4047 views |
  • submit to reddit
I'm a big fan of using PHPUnit with console colors turned on (using the --colors option). Eg:

/static/images/screenshots/phpunit.jpg


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")
    else:
        print
        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:

/static/images/screenshots/fab.jpg



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

Published at DZone with permission of David Winterbottom, author and DZone MVB.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)