The psychology of program computing
Speed

std.utest, the complementary unit testing framework

Holger Krekel and Armin Rigo have been working on a "complementary second standard library" for Python. They want to make the standard library more Pythonic, rather than Java-like. Their motto is that "the best API is the one that doesn't exist."

And, indeed, their unit testing framework, std.utest, is mostly non-existent to the application programmer. Consider this simple routine, in a module named, say, list.py:

 def matching_names(prefix, names):
     """Case insensitive match."""
     
     prefix = prefix.upper()
     return [name for name in names
             if name.upper().startswith(prefix)]
 
Now, to test it with std.utest we make a module named test_list.py, and its complete listing is this:
 from list import matching_names
 

def test_matching_names(): names = ["abc", "Abac", "baca", "bbb"] assert matching_names("a", names) == ["abc", "Abac"] assert matching_names("ab", names) == ["abc", "Abac"] assert matching_names("ba", names) == ["bac"]

No special imports, no classes (although you can use a class to organize your tests if you want), no inheritance, no test runners, no suites. Running this with std.utest's supplied test runner (you can write your own if necessary) is as simple as this:
 python ..\std\bin\utest
 
Which spits out this:
 inserting C:\Documents and Settings\Jarno Virtanen
 _______________________________________________________________________________
 ______________________________  TESTS STARTING  ______________________________
 _______________________________________________________________________________
 F
 

_______________________________________________________________________________ ________________________________ Test Failure ________________________________

>>> test_matching_names >>> 'C:\\Documents and Settings\\Jarno Virtanen\\cd\\test_list.py', line 9

def test_matching_names(): names = ["abc", "Abac", "baca", "bbb"] assert matching_names("a", names) == ["abc", "Abac"] assert matching_names("ab", names) == ["abc", "Abac"] > assert matching_names("ba", names) == ["bac"]

------------------------------------------------------------------------------- assert ['baca'] == ['bac'] + where ['baca'] = matching_names('ba', ['abc', 'Abac', 'baca', 'bbb']) ============================== 1 TESTS FINISHED ============================== 0.05 seconds (0 passed, 1 failed, 0 skipped)

Notice how std.utest magically deduces what exactly was compared, in the runtime, in the failing assertion. It also shows how the tested routine was called by.

Unfortunately, no one has yet written much documentation about the complementary standard library. You pretty much have to figure it out yourself, but, as you saw, it's not too difficult once the basic idea is revealed.

The only way (that I know of) to obtain it right now is to use Subversion and check out the project like this:

 svn co http://codespeak.net/svn/std/trunk/src/std
 

[2 comments]

Filed under:

Comments:

Posted by Ian Bicking at 08.08.2004, 21:34

Hrm... not only isn't there documentation, there doesn't seem to be any setup.py. Once I copied std into my path, I also had to add a line to the bottom of bin/_findstd.py that searched os.path.dirname(__file__), and then it worked.

There also doesn't seem to be any mailing list or particular contact address, except Holger and Armin directly I suppose.

Posted by Jarno Virtanen at 09.08.2004, 08:20

Yeah, I should have had mentioned that the installation is a bit clunky too. Judging by the archives of pypy-dev it seems that Holger et al are too busy with other things right now, but they are planning to make a decent release of the std stuff later.