18

Is there a way to colorize the Django test output? Basically a Red/Green factor for a pass or failing results?

I am on Mac OS X using Terminal.app.

A R
  • 1,412
  • 2
  • 14
  • 29
jeffci
  • 2,537
  • 6
  • 37
  • 59

7 Answers7

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
  • 243
  • 2
  • 9
  • Actually the package is redgreenunittest (even though the git repo has an "s"), so you'll want to run `pip install redgreenunittest`. – Greg Kaleka Oct 10 '17 at 15:59
6

I found pyrg to work quite well:

pyrg manage.py test

The required command can be installed with pip:

pip install pyrg
Ropez
  • 3,485
  • 3
  • 28
  • 30
  • This didnt work for me on python3x. on OSX..pip install pyrg Collecting pyrg Downloading https://files.pythonhosted.org/packages/5c/e0/ed29cec7b1ffae0ca97f0f560b8bf2265d42ead6047adcb759a1b0af6870/pyrg-0.2.6.tar.gz Complete output from command python setup.py egg_info: File "", line 1, in File "/private/var/folders/0w/9y11s6qj6tvg2q3rzvgn7ytr0000gn/T/pip-install-armngvjd/pyrg/setup.py", line 7, in import pyrg File "pyrg/pyrg.py", line 226 ^ – Stryker Oct 05 '18 at 18:17
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.

Brad Montgomery
  • 2,621
  • 1
  • 24
  • 24
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...

supervacuo
  • 9,072
  • 2
  • 44
  • 61
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
  • 791
  • 6
  • 6
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.

Community
  • 1
  • 1
Kenny Shen
  • 4,773
  • 3
  • 21
  • 18
1

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

Community
  • 1
  • 1
zVictor
  • 3,610
  • 3
  • 41
  • 56