unittest colored output

PythonUnit Testing

Python Problem Overview


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)

Python Solutions


Solution 1 - Python

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

Solution 2 - Python

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

Solution 3 - Python

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

Solution 4 - Python

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.

Solution 5 - Python

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

Solution 6 - Python

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

Solution 7 - Python

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.

Solution 8 - Python

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)

Solution 9 - Python

Try rudolf plugin for nosetests.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndreaView Question on Stackoverflow
Solution 1 - PythonmeshyView Answer on Stackoverflow
Solution 2 - PythonDavidView Answer on Stackoverflow
Solution 3 - PythonjohnbaumView Answer on Stackoverflow
Solution 4 - PythonrobertView Answer on Stackoverflow
Solution 5 - PythonHarlemSquirrelView Answer on Stackoverflow
Solution 6 - PythonJean Carlo MachadoView Answer on Stackoverflow
Solution 7 - PythonSteveView Answer on Stackoverflow
Solution 8 - PythonIgnoredAmbienceView Answer on Stackoverflow
Solution 9 - PythonmiahView Answer on Stackoverflow