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

Guido van Rossum guido@python.org
Mon, 19 Aug 2002 11:51:27 -0400


> > > 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.

Aha.  The problem is in the framework you're using, not in
unittest.py.

I find it totally reasonable that the framework should flag *some*
kind of error when a purported test module fails to import correctly.

> 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.

The test.py we use for Standalone ZODB and Zope3 already has the
necessary machinery to warn about failing imports.

To me, the import-in-setUp method has the disadvantage that your test
cases can't reference to the tested module by its normal, "global"
name -- they have to get it from an instance variable on the test
instance.

Or are you using

    def setUp(self):
        global fooModule
        import fooModule

???

--Guido van Rossum (home page: http://www.python.org/~guido/)