-
Notifications
You must be signed in to change notification settings - Fork 3
/
testlayer.txt
115 lines (92 loc) · 3.87 KB
/
testlayer.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
Layers
======
zope.app.appsetup.testlayer define a test layer which creates a test
database.
ZODBLayer
---------
We can instantiate a ZODBLayer::
>>> from zope.app.appsetup.testlayer import ZODBLayer
>>> from zope.app.appsetup import testpackage
>>> layer = ZODBLayer(testpackage)
>>> layer
<zope.app.appsetup.testlayer.ZODBLayer object at ...>
Now we run some tests with this layer that checks that we have a
working database::
>>> import unittest
>>> import transaction
>>> from zope import component
>>> from zope.app.appsetup.testpackage import testobject
>>> from ZODB.interfaces import IDatabase
>>> class TestCase(unittest.TestCase):
... layer = layer
...
... def testAddObjectInDB(self):
... root = self.layer.getRootFolder()
... root['object'] = testobject.TestObject()
... transaction.commit()
... self.assertIn('object', root)
... def testNoMoreObjectInDB(self):
... root = self.layer.getRootFolder()
... self.assertNotIn('object', root)
... def testApplicationInDB(self):
... root = self.layer.getRootFolder()
... self.assertEqual(
... repr(root.__class__), "<class 'zope.site.folder.Folder'>")
... def testDBRegistered(self):
... root = self.layer.getRootFolder()
... db = component.getUtility(IDatabase, name='main')
... self.assertEqual(db, root._p_jar.db())
We define a suite with our test:
>>> suite = unittest.TestSuite()
>>> suite.addTest(unittest.makeSuite(TestCase))
And run that suite:
>>> from zope.testrunner.runner import Runner
>>> runner = Runner(args=[], found_suites=[suite])
>>> succeeded = runner.run()
Running zope.app.appsetup.testpackage.ZODBLayer tests:
Set up zope.app.appsetup.testpackage.ZODBLayer in ... seconds.
Ran 4 tests with 0 failures, 0 errors and 0 skipped in ... seconds.
Tearing down left over layers:
Tear down zope.app.appsetup.testpackage.ZODBLayer in ... seconds.
Database stacking with ZODBlayer
--------------------------------
When deriving from ZODBLayer we can start populating a test database at layer
setup time. Each test will be run with a DemoStorage wrapped around that
baseline and unwrapped afterwards. This way you can save a significant amount
of time if tests require shared setup.
We start with a derived layer, based on ZODBLayer that has some setup code:
>>> class CustomZODBLayer(ZODBLayer):
... def setUp(self):
... super(CustomZODBLayer, self).setUp()
... root = self.getRootFolder()
... root['foo'] = 1
... transaction.commit()
>>> layer = CustomZODBLayer(testpackage)
>>> layer.__class__
<class 'CustomZODBLayer'>
Now, when running tests in this layer, each test sees the prepopulated
database but is still isolated from other tests' changes:
>>> class CustomZODBLayerTests(unittest.TestCase):
... layer = layer
... def test_test1(self):
... root = self.layer.getRootFolder()
... self.assertEqual(1, len(root))
... root['bar'] = 1
... transaction.commit()
... def test_test2(self):
... root = self.layer.getRootFolder()
... self.assertEqual(1, len(root))
... root['baz'] = 1
... transaction.commit()
Run the tests to prove that above statements are correct::
>>> suite = unittest.TestSuite()
>>> suite.addTest(unittest.makeSuite(CustomZODBLayerTests))
And run that suite:
>>> from zope.testrunner.runner import Runner
>>> runner = Runner(args=[], found_suites=[suite])
>>> succeeded = runner.run()
Running zope.app.appsetup.testpackage.CustomZODBLayer tests:
Set up zope.app.appsetup.testpackage.CustomZODBLayer in ... seconds.
Ran 2 tests with 0 failures, 0 errors and 0 skipped in ... seconds.
Tearing down left over layers:
Tear down zope.app.appsetup.testpackage.CustomZODBLayer in ... seconds.