52

I use unittest (actually unittest2) for Python testing, together with Python Mock for mocking objects and nose to run all tests in a single pass.

I miss being able to tell what is working and what's wrong at a glance from the green/red bars. Is there a way to get colored output from unittest?

(Changing test suite at this point is not an option, and I actually like unittest)

Andrea
  • 20,253
  • 23
  • 114
  • 183

9 Answers9

31

Using a method very similar to robert's answer, I have (today!) released a package that enables colour output in unittest test results. I have called it colour-runner.

To install it, run:

pip install colour-runner

Then, where you were using unittest.TextTestRunner, use colour_runner.runner.ColourTextTestRunner instead.

See how it looks with verbosity=1...and verbosity=2

Community
  • 1
  • 1
meshy
  • 8,470
  • 9
  • 51
  • 73
  • What platforms does this work on? I only ask because colors in terminals on Windows is very different from colors in terminals on Linux and OS X. – robert Mar 11 '14 at 12:21
  • That's a good question. I have tested it on linux and OS X, but I don't have a Windows machine to test it on. I use two libraries to do the output. The [blessings](https://github.com/erikrose/blessings/) library claims it "should work when used in concert with [colorama](https://pypi.python.org/pypi/colorama)" on windows, so it may need that extra package. I can't find any mention of whether [pygments](http://pygments.org/) supports windows, but I suspect that if it doesn't, colorama may resolve the issue. I would be very interested to know how this works on windows. – meshy Mar 11 '14 at 20:08
  • Can I please have an explanation of the downvote so that I can make improvements? The downvote on its own doesn't very well lend itself to constructive action. – meshy Dec 04 '14 at 13:55
  • Is it possible to use this with nosetests, along the lines of rednose? – naught101 Jan 22 '15 at 06:17
  • @naught101 I'm pretty sure that this will not work with `nose`. – meshy Jan 22 '15 at 09:50
  • Just tried on Windows, but it doesn't work since it needs the curses library. – Giancarlo Sportelli Mar 28 '19 at 18:42
  • What if my unittest ends with: if __name__ == '__main__': unittest.main(verbosity=2). How to modify this to imply color please? – chikitin Feb 22 '20 at 02:55
  • Does not work! any hits? def suite(): suite = unittest.TestSuite() suite.addTest(WidgetTestCase('Test_Network2')) return suite if __name__ == '__main__': runner = colour_runner.runner.ColourTextTestRunner() runner.run(suite()) – chikitin Feb 22 '20 at 03:09
21

I'm having good success with nosetests and rednose. It's still maintained at the time of writing this.

David
  • 9,635
  • 5
  • 62
  • 68
13

In python 2.x you could try pyrg. Does not work in Python 3 though.

BookOfGreg
  • 3,550
  • 2
  • 42
  • 56
johnbaum
  • 664
  • 4
  • 5
  • This is definitely the easiest solution. – Andrea Mar 05 '11 at 19:10
  • I found the latest version (0.2.6), if run in command line mode, only waits 1ms for data on stdin - if it doesnt receive any then it falls over with a usage message. It's easy enough to change the poll timeout to a more-reasonable 1000ms – Tom Dalton Apr 07 '14 at 16:29
12

Make a class that inherits from unittest.TestResult (say, MyResults) and implements a bunch of methods. Then make a class that inherits from unittest.TextTestRunner (say, MyRunner) and override _makeResult() to return an instance of MyResults.

Then, construct a test suite (which you've probably already got working), and call MyRunner().run(suite).

You can put whatever behavior you like, including colors, into MyResults.

robert
  • 33,242
  • 8
  • 53
  • 74
9

If you are running pytest this way:

python -m unittest test_my.py

Change it to:

pytest test_my.py

And you get colors for free

Jean Carlo Machado
  • 1,480
  • 1
  • 15
  • 25
9

pytest can do this with no changes needed for unit tests.

enter image description here

Now install pytest.

pip install --user pytest

And run the tests to see the color!

enter image description here

HarlemSquirrel
  • 8,966
  • 5
  • 34
  • 34
3

If you could change just the line of your test imports, you could use redgreenunittest. It's a clone I made of unittest, but it has colorized output.

If you want to use it without updating any of the meat of your code, you can just use it like so:

import redgreenunittest as unittest

It's not a clone of unittest2, so it wouldn't work out-of-the-box with Andrea's code, but its source is right there, so a unittest2 fork of redgreenunittest wouldn't be out of the question.

Also, any "you're doing it wrong" comments are welcome, so long as they contain some reasoning. I'd love to do it right instead.

Steve
  • 791
  • 6
  • 6
2

I've also found another colouring plugin for nose: YANC at https://pypi.python.org/pypi/yanc

Works for me with Python 3.5 and nose 1.3.7 (I couldn't get any of the other options for nose listed above to work)

IgnoredAmbience
  • 103
  • 1
  • 8
1

Try rudolf plugin for nosetests.

miah
  • 8,190
  • 2
  • 24
  • 23