[Fixed]-Colorizing the output of Django tests

3👍

If you’re already using Fabric for deployment, you can use this snippet from @codeinthehole’s blog post:

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

It doesn’t colorise the individual test outputs, but it does give you immediate red / green satisfaction…

21👍

redgreenunittests is the most simple solution and it works great with python 3.x

Install it

pip install redgreenunittest

add the next line into settings.py

TEST_RUNNER = "redgreenunittest.django.runner.RedGreenDiscoverRunner"

and don’t forget to enjoy 🙂

./manage test

👤injaon

6👍

I found pyrg to work quite well:

pyrg manage.py test

The required command can be installed with pip:

pip install pyrg
👤Ropez

4👍

I know this is an old question, but django-rainbowtests aims to do this. Failures and Errors are Red, Success is green, and it highlights your project’s code in larger stack traces.

3👍

If you’re not using Fabric, you might like redgreenunittest. Basically, you just put it in the appropriate place in your project (probably in your virtual environment), and then reference it as your TEST_RUNNER in your settings like this:

TEST_RUNNER="redgreenunittest.django.simple.RedGreenTestSuiteRunner"

If you’re only using Django’s test helper code (mostly django.test.TestCase), then that should do it. otherwise you may need to reference redgreenunittest directly like so:

import redgreenunittest as unittest

Then you just run your tests. And they’ll have colors. Like magic.

👤Steve

2👍

Take a look at Print in terminal with colors using Python?. You should be able to modify or roll out your own colorization from there.

1👍

I found a possible solution, called pyrg, in this question.
Unfortunately, didn’t work as expected for me.

Leave a comment