184
188
>>> print exc_info[1].args[0][0]
185
189
<type 'exceptions.ZeroDivisionError'>
194
A common use case within complex environments is having some fixtures shared by
197
Consider the case of testing using a ``TempDir`` with two fixtures built on top
198
of it; say a small database and a web server. Writing either one is nearly
199
trivial. However handling ``reset()`` correctly is hard: both the database and
200
web server would reasonably expect to be able to discard operating system
201
resources they may have open within the temporary directory before its removed.
202
A recursive ``reset()`` implementation would work for one, but not both.
203
Calling ``reset()`` on the ``TempDir`` instance between each test is probably
204
desirable but we don't want to have to do a complete ``cleanUp`` of the higher
205
layer fixtures (which would make the ``TempDir`` be unused and trivially
206
resettable. We have a few options available to us.
208
Imagine that the webserver does not depend on the DB fixture in any way - we
209
just want the webserver and DB fixture to coexist in the same tempdir.
211
A simple option is to just provide an explicit dependency fixture for the
212
higher layer fixtures to use. This pushes complexity out of the core and onto
215
>>> class WithDep(fixtures.Fixture):
216
... def __init__(self, tempdir, dependency_fixture):
217
... super(WithDep, self).__init__()
218
... self.tempdir = tempdir
219
... self.dependency_fixture = dependency_fixture
221
... super(WithDep, self).setUp()
222
... self.addCleanup(self.dependency_fixture.cleanUp)
223
... self.dependency_fixture.setUp()
224
... # we assume that at this point self.tempdir is usable.
226
>>> WebServer = WithDep
227
>>> tempdir = fixtures.TempDir()
228
>>> db = DB(tempdir, tempdir)
229
>>> server = WebServer(tempdir, db)
233
Another option is to write the fixtures to gracefully handle a dependency
234
being reset underneath them. This is insufficient if the fixtures would
235
block the dependency resetting (for instance by holding file locks open
236
in a tempdir - on Windows this will prevent the directory being deleted).
238
Another approach which ``fixtures`` neither helps nor hinders is to raise
239
a signal of some sort for each user of a fixture before it is reset. In the
240
example here, ``TempDir`` might offer a subscribers attribute that both the
241
DB and web server would be registered in. Calling ``reset`` or ``cleanUp``
242
on the tempdir would trigger a callback to all the subscribers; the DB and
243
web server reset methods would look something like:
246
... if not self._cleaned:
249
(Their action on the callback from the tempdir would be to do whatever work
250
was needed and set ``self._cleaned``.) This approach has the (perhaps)
251
suprising effect that resetting the webserver may reset the DB - if the
252
webserver were to be depending on ``tempdir.reset`` as a way to reset the
255
Another approach which is not currently implemented is to provide an object
256
graph of dependencies and a reset mechanism that can traverse that, along with
257
a separation between 'reset starting' and 'reset finishing' - the DB and
258
webserver would both have their ``reset_starting`` methods called, then the
259
tempdir would be reset, and finally the DB and webserver would have
260
``reset_finishing`` called.
227
311
>>> fixture = fixtures.PythonPackage('foo.bar', [('quux.py', '')])
316
Adds a single directory to sys.path. If the directory is already in the path,
317
nothing happens, if it isn't then it is added on setUp and removed on cleanUp.
319
>>> fixture = fixtures.PythonPathEntry('/foo/bar')
232
324
Create a temporary directory and clean it up later.
234
326
>>> fixture = fixtures.TempDir()
328
The created directory is stored in the ``path`` attribute of the fixture after