4
Normally when you create a class in a doctest, it will have the
5
``__module__`` attribute of ``'__builtin__'``. This is sometimes not
6
desirable. Let's demonstrate the behavior::
14
By using ``zope.testing.module.setUp`` this can be
15
controlled. Normally you set up your tests with it, but in this case
16
we'll just call it manually.
18
To call this function manually, we need to set up a fake ``test``
19
object. This because the ``setUp`` function expects a test with at
20
least the ``globs`` dictionary attribute being present. Let's make
21
such a fake test object, using the globals of the doctest::
23
>>> class FakeTest(object):
24
... def __init__(self):
25
... self.globs = globals()
29
We can now call the ``setUp`` function::
31
>>> from zope.testing.module import setUp
34
We will now demonstrate that the ``__module__`` argument is something
35
else, in this case the default, ``__main__``::
37
>>> class Foo(object):
42
Let's tear this down again::
44
>>> from zope.testing.module import tearDown
47
We should now be back to the original situation::
49
>>> class Foo(object):
57
Let's now imagine a more complicated example, were we actually want to
58
be able to import the fake module as well::
60
>>> setUp(test, 'fake')
63
The import should not fail::
69
Let's tear it down again::
73
Traceback (most recent call last):
75
ImportError: No module named fake
77
If we enter a dotted name, it will actually try to place the fake
78
module in that dotted name::
80
>>> setUp(test, 'zope.testing.unlikelymodulename')
82
>>> import zope.testing.unlikelymodulename
83
>>> zope.testing.unlikelymodulename.a
85
>>> from zope.testing import unlikelymodulename
86
>>> unlikelymodulename.a
89
>>> import zope.testing.unlikelymodulename
90
Traceback (most recent call last):
92
ImportError: No module named unlikelymodulename
94
This only works for packages that already exist::
96
>>> setUp(test, 'unlikelynamespacename.fake')
97
Traceback (most recent call last):
99
KeyError: 'unlikelynamespacename'
101
Even so, we still need to tear down::
104
Traceback (most recent call last):
106
KeyError: 'unlikelynamespacename'