unittest colored output
Asked Answered
F

9

53

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)

Frye answered 5/3, 2011 at 11:53 Comment(1)
what did you use before that had the green/red bars?Plantar
V
13

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

Vaporish answered 5/3, 2011 at 12:22 Comment(2)
This is definitely the easiest solution.Frye
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 1000msNautch
C
32

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

Cruz answered 5/3, 2014 at 22:45 Comment(8)
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.Countercurrent
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 library claims it "should work when used in concert with colorama" on windows, so it may need that extra package. I can't find any mention of whether pygments 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.Cruz
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.Cruz
Is it possible to use this with nosetests, along the lines of rednose?Episcopacy
@Episcopacy I'm pretty sure that this will not work with nose.Cruz
Just tried on Windows, but it doesn't work since it needs the curses library.Lilybelle
What if my unittest ends with: if name == 'main': unittest.main(verbosity=2). How to modify this to imply color please?Certie
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())Certie
C
21

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

Cessionary answered 3/7, 2012 at 14:52 Comment(2)
easy_install failed for me, but pip worked fine: sudo pip install rednoseJahdol
This works for me, nosetests --verbosity=2 --rednose ./, I'm using Python 3.6.8 and Nose 1.3.7. Thank you!Cordiacordial
V
13

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

Vaporish answered 5/3, 2011 at 12:22 Comment(2)
This is definitely the easiest solution.Frye
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 1000msNautch
C
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.

Countercurrent answered 5/3, 2011 at 17:9 Comment(0)
U
10

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

Uziel answered 23/2, 2019 at 5:35 Comment(0)
L
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

Leftover answered 7/1, 2020 at 21:43 Comment(0)
B
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.

Borghese answered 11/5, 2013 at 14:29 Comment(0)
B
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)

Berardo answered 3/11, 2015 at 18:48 Comment(1)
This should be the accepted answer ^^ The color is lovely and best among other nose pluginsFiduciary
V
1

Try rudolf plugin for nosetests.

Velodrome answered 5/3, 2011 at 12:23 Comment(1)
No python 3 version :/Episcopacy

© 2022 - 2024 — McMap. All rights reserved.