Unittest - How do I code lots of simple tests

Jeremy Hylton jeremy at zope.com
Wed Oct 22 13:30:31 EDT 2003


test.py isn't a model of good software engineering, so I'm not surprised
that you're having trouble figuring out how to put your tests in the right
place.  Zope has a fairly rigid policy about where to put tests, but it
wouldn't be hard to make that policy more flexible.  It's really a question
of what policies you want to support.  We're happy with the Zope policy, so
we haven't thought much about other policies.

That said, the policy is... A package Foo stores all its tests in a
subpackage named Foo.tests.  Any module in Foo.tests whose name starts with
"test" may contain test suites.  For example, Foo.tests.testFoo might
contain tests.  Foo.tests.spamFoo would be ignored.  The test driver finds
the potential test modules by searching the file system.

Once it finds the potential test modules, it imports each one and looks for
a function called test_suite().  If that function exists, it should return a
unittest.TestSuite object.  The test driver collects all of these tests in a
single mondo TestSuite that it actually runs.

There are two regular expressions that are used to filter this list.
Regular expressions are usually overkill, but occasionally useful.  Most of
the time, I just depend on regexps to do string matching.  One time when I
use regexps is to ignore a certain set of tests like "!^checkPack".

test.py takes two optional positional arguments.  The first is a regexp to
match the name of the test module, the second is a regexp to match the name
of a particular test.  The first regexp matches against the name
"Foo.tests.testFoo."  The second regexp would match against "checkFooFails."

Hope that helps,
Jeremy

-----Original Message-----
From: python-list-bounces+jeremy=zope.com at python.org
[mailto:python-list-bounces+jeremy=zope.com at python.org]On Behalf Of Ian
Bicking
Sent: Wednesday, October 22, 2003 12:38 PM
To: John J. Lee
Cc: python-list at python.org
Subject: Re: Unittest - How do I code lots of simple tests


On Wednesday, October 22, 2003, at 08:05 AM, John J. Lee wrote:
> Ian Bicking <ianb at colorstudy.com> writes:
> [grumbles about unittest]
>
> BTW, Ian, did you notice that somewhere on one of the Zope sites
> there's a reference to your blog entry about this?  Looked like they
> have a unittest.main-replacement.

Yes, I've been experimenting with it.  (For others, the URL is
http://cvs.zope.org/Zope/test.py ) -- it's a bit hard to figure out, it
has some documentation but only about how to run the script, not how to
organize your tests.

After much struggling I've gotten it to run -- it seems picky about
where you put your tests, and it just ignored my tests until I more
carefully pointed the script to where they were (but there's several
ways to point the script to a directory, only --libdir seemed to work
for me).  I'm still not sure how to specify which tests I want to run
-- it has some regex control there, which seems like both overkill and
underkill (I don't need the flexibility of regexes to match modules,
but I want better ways to indicate specific tests in those modules).  I
actually *like* the basic interface of unittest.main.

What appealed to me about Zope's test.py was pychecker and pdb options
(ontop of code coverage, which several frameworks have).  Though once I
looked at how pychecker is invoked, that's easy to add to any
framework.  Code coverage is supported by several frameworks -- I added
it to my own unittest-based tests pretty easily too.  Maybe if I looked
at pdb it would also be easy enough as well, at which point I may or
may not find the Zope test.py appealing enough to try to figure out how
it finds (or does not find) files (though now that I've gotten this
far...)

--
Ian Bicking | ianb at colorstudy.com | http://blog.ianbicking.org


--
http://mail.python.org/mailman/listinfo/python-list






More information about the Python-list mailing list