Archive for the 'Python' Category

Note: I've reorganized this site to use tags; the category archive remains to support old links. Only posts prior to April, 2006 are categorized. Tag Archive »

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.

And Now For Something Completely Different…

Just becuase I don’t have enough diverse (and conflicting) interests, I’m currently learning Python. So far I like what I see. My only gripe is that the version of Python 2.3 Apple shipped with Panther does not support command line editing. In other words, in interactive Python, you can’t use up-arrow to access a command history (you have no history, except cut-and-paste, which means hands off the keyboard/on to the mouse), or use left-arrow to edit the current line (delete ’em and weep). I’m pretty sure that this can be compiled into Python, I don’t understand why Apple didn’t.

Also of note, I’m learning Python via Learning Python (go figure), which I’m reading via Safari Bookshelf. Although I really like dead tree editions, and generally prefer reading hardcopy to electronic copy, I have to admit that it’s working out pretty well. It’s especially a good choice for this book, for two reasons. The second edition is due out next month… no sense spending the money for a hardcopy of the current edition. Also, I’m working through many of the exercises in the book, so I’m at the keyboard anyway. I originally signed up for Bookshelf as a way to keep reference books at hand and audition books to buy, but so far reading a whole book online is working well.