[Zope-Coders] Help with Unit Testing: safe imports

Tres Seaver tseaver@zope.com
19 Aug 2002 11:10:11 -0400


On Mon, 2002-08-19 at 10:01, Guido van Rossum wrote:
> > Python's own 'unittest.py' fails silently if the test module bails with
> > import errors.
> > 
> > Successful import of the module-under-test should *not* be assumed by
> > the test module..
> 
> I don't understand this.  Can you describe a scenario in more detail?

Constructing a suite of tests for an entire package is tricky:  what is
the framework supposed to do if it encounters a module which looks like
a test (has the correct name, imports unittest, etc.) but which raises
an ImportError at module scope?  The tests which *should* have been run
are unavailable, due to the partial import, so the reporting machinery
can't log them as failed tests.  The execution environment *may* try to
report the problem, but the "standard" ones do not do so.  For instance,
with module-scope imports, the stock unittestgui.py will show a "green
bar", with a reduced number of passing tests.

The idiom I propose (import the module-under-test only within the
context of 'testFoo' method) has netiher disadvantage:  import errors
are caught and reported as test failures, without any need for
additional reporting machinery.

Tres.
-- 
===============================================================
Tres Seaver                                tseaver@zope.com
Zope Corporation      "Zope Dealers"       http://www.zope.com