Python unittest Greenbar Without the GUI

Lately I’ve been learning Python (again), and trying out Unit Testing using Python’s unittest module. In many unit testing tools, each time the test suite is run, a progress bar is shown, which is green as long as the tests pass, but turns red when a test fails. Passing your tests is called “getting a greenbar,” and is the mark of success.

I’ve been working on my Powerbook, using the default Python install. I work with two terminal windows open; one running Emacs, and the other to run my tests in. The unittest module provides a command line method of running all tests, with output appearing on the terminal. I wanted to have a greenbar/redbar indicator, but none is present.

There is a GUI version, unittestgui, available which uses tkinter, but doesn’t ship with OS X. Google turned up a Cocoa app for OS X for running Python unit tests, but it hasn’t been updated in a few years, and just crashes on my Powerbook.

Since I prefer to develop in a terminal session anyway, I decided I wanted a way to run my unit tests from the shell, and see the red/green effect right in the terminal. Thus I created utest, a Bash script that runs a unittest, and changes the display text color using ANSI escape codes based on the output of the test. Since unittest prints all testing output to STDERR, I redirect it to STDOUT and pipe the the whole shooting match into perl one-liner that parrots eveything, while setting colors as appropriate. Output starts green, but turns red if the output contains FAIL or ERROR. It also unbuffers standard out, so the results appear correctly.

Here it is in action, when all tests pass:

utest with passed tests

And here it is, with a failing test:

utest with failed test

If you can’t see these images, you’re probably using IE, so don’t come crying to me. You know what to do. If you’re wondering about my tabbed terminal, it’s iTerm.

Both comments and pings are currently closed.

3 Responses to “Python unittest Greenbar Without the GUI”

  1. effika Says:

    Test 3 comes before test 2!

  2. Jason Says:

    Under unittest (and most xUnit implementations), the order that the tests run is not defined, or is arbitrary (in Perl’s Test::Class, for example, tests are run alphabetically). This is largely because each test case is a method of a class, and the introspection mechanism that locates the tests can’t tell what order the tests were defined in. It also reflects the fact that each test should be isolated, free from interaction with other tests- order should not matter.

    Having said all that, I guess it wasn’t too bright of me to name the tests in my samples “Test 1”, “Test 2”, and “Test 3”.

  3. Ori Peleg Says:

    TestOOB does that for you, and more<br/>

    You may like TestOOB, an extension for Python unit testing. It works with your unittest suites with no changes and adds some nice features, like colorized console output with a ‘–color’ command-line option.