~landscape/zope3/newer-from-ztk

« back to all changes in this revision

Viewing changes to src/zope/testing/module.txt

  • Committer: Thomas Hervé
  • Date: 2009-09-21 16:46:07 UTC
  • Revision ID: thomas@canonical.com-20090921164607-sky3xhlt02ji80ka
Revert r8: regression with test failures

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Module setup
2
 
============
3
 
 
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::
7
 
 
8
 
  >>> class Foo(object):
9
 
  ...    pass
10
 
 
11
 
  >>> Foo.__module__
12
 
  '__builtin__'
13
 
 
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.
17
 
 
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::
22
 
 
23
 
  >>> class FakeTest(object):
24
 
  ...     def __init__(self):
25
 
  ...        self.globs = globals()
26
 
 
27
 
  >>> test = FakeTest()
28
 
 
29
 
We can now call the ``setUp`` function::
30
 
 
31
 
  >>> from zope.testing.module import setUp
32
 
  >>> setUp(test)
33
 
 
34
 
We will now demonstrate that the ``__module__`` argument is something
35
 
else, in this case the default, ``__main__``::
36
 
 
37
 
  >>> class Foo(object):
38
 
  ...     pass
39
 
  >>> Foo.__module__
40
 
  '__main__'
41
 
 
42
 
Let's tear this down again::
43
 
 
44
 
  >>> from zope.testing.module import tearDown
45
 
  >>> tearDown(test)
46
 
 
47
 
We should now be back to the original situation::
48
 
 
49
 
  >>> class Foo(object):
50
 
  ...    pass
51
 
  >>> Foo.__module__
52
 
  '__builtin__'
53
 
 
54
 
Importing
55
 
---------
56
 
 
57
 
Let's now imagine a more complicated example, were we actually want to
58
 
be able to import the fake module as well::
59
 
 
60
 
  >>> setUp(test, 'fake')
61
 
  >>> a = 'Hello world'
62
 
 
63
 
The import should not fail::
64
 
 
65
 
  >>> import fake
66
 
  >>> fake.a
67
 
  'Hello world'
68
 
 
69
 
Let's tear it down again::
70
 
 
71
 
  >>> tearDown(test)
72
 
  >>> import fake
73
 
  Traceback (most recent call last):
74
 
    ...
75
 
  ImportError: No module named fake
76
 
 
77
 
If we enter a dotted name, it will actually try to place the fake
78
 
module in that dotted name::
79
 
 
80
 
  >>> setUp(test, 'zope.testing.unlikelymodulename')
81
 
  >>> a = 'Bye world'
82
 
  >>> import zope.testing.unlikelymodulename
83
 
  >>> zope.testing.unlikelymodulename.a
84
 
  'Bye world'
85
 
  >>> from zope.testing import unlikelymodulename
86
 
  >>> unlikelymodulename.a
87
 
  'Bye world'
88
 
  >>> tearDown(test)
89
 
  >>> import zope.testing.unlikelymodulename
90
 
  Traceback (most recent call last):
91
 
    ...
92
 
  ImportError: No module named unlikelymodulename
93
 
 
94
 
This only works for packages that already exist::
95
 
 
96
 
  >>> setUp(test, 'unlikelynamespacename.fake')
97
 
  Traceback (most recent call last):
98
 
    ...
99
 
  KeyError: 'unlikelynamespacename'
100
 
 
101
 
Even so, we still need to tear down::
102
 
 
103
 
  >>> tearDown(test)
104
 
  Traceback (most recent call last):
105
 
    ...
106
 
  KeyError: 'unlikelynamespacename'