[Zope-Coders] Help with Unit Testing: safe imports
Casey Duncan
casey@zope.com
Mon, 19 Aug 2002 09:16:24 -0400
We need a unit testing "Best Practices" wiki or something. This is making=
my=20
brain hurt ;^)
-Casey
On Monday 19 August 2002 09:02 am, Tres Seaver wrote:
> On Mon, 2002-08-19 at 06:57, Chris Withers wrote:
> > Shane Hathaway wrote:
> > >=20
> > > Put the imports in the test methods or the setUp() method.
> >=20
> > They don't work if you put them in the setUp() method.
> > so, lots of duplicates in the test methods it is then :-(
>=20
> Imports within the 'setUp' are dangerous if that method is doing
> anything which might "leak" if 'tearDown' isn't called (e.g., opening
> database connections). The idiom I use for this is to add helper metho=
d
> (.e.g., '_makeOne'), which imports the module-under-test and
> instantiates on object. In this mode, import failures generate test
> failures, but 'setUp' / 'tearDown' will always be called and completed.=
=20
> E.g.:
>=20
> class FooTests( unittest.TestCase ):
>=20
> def _makeOne( self, *args, **kw ):
>=20
> from Products.FooProduct.Foo import Foo
> return Foo( *args, **kw )
>=20
> def testSomething( self ):
>=20
> foo =3D self._makeOne()
>=20
> #....
>=20
> Tres.
> --=20
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Tres Seaver tseaver@zope.com
> Zope Corporation "Zope Dealers" http://www.zope.com
>=20
>=20
> _______________________________________________
> Zope-Coders mailing list
> Zope-Coders@zope.org
> http://lists.zope.org/mailman/listinfo/zope-coders
>=20