• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

zopefoundation / zope.component / 4686002484

pending completion
4686002484

push

github

Michael Howitz
Fix too long line.

460 of 475 branches covered (96.84%)

Branch coverage included in aggregate %.

4646 of 4646 relevant lines covered (100.0%)

1.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

98.08
/src/zope/component/testlayer.py
1
##############################################################################
2
#
3
# Copyright (c) 2010 Zope Foundation and Contributors.
4
# All Rights Reserved.
5
#
6
# This software is subject to the provisions of the Zope Public License,
7
# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
8
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
9
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
10
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
11
# FOR A PARTICULAR PURPOSE.
12
#
13
##############################################################################
14

15
import os
1✔
16

17
from zope.configuration import config
1✔
18
from zope.configuration import xmlconfig
1✔
19

20

21
try:
1✔
22
    from zope.testing.cleanup import cleanUp
1✔
23
except ImportError:  # pragma: no cover
24
    def cleanUp():
25
        pass
26

27
from zope.component import provideHandler
1✔
28
from zope.component.eventtesting import clearEvents
1✔
29
from zope.component.eventtesting import events
1✔
30
from zope.component.hooks import setHooks
1✔
31

32

33
class LayerBase:
1✔
34
    """Sane layer base class.
35

36
    zope.testing implements an advanced mechanism so that layer setUp,
37
    tearDown, testSetUp and testTearDown code gets called in the right
38
    order. These methods are supposed to be @classmethods and should
39
    not use super() as the test runner is supposed to take care of that.
40

41
    In practice, this mechanism turns out not to be useful and
42
    overcomplicated.  It becomes difficult to pass information into
43
    layers (such as a ZCML file to load), because the only way to pass
44
    in information is to subclass, and subclassing these layers leads
45
    to a range of interactions that is hard to reason about.
46

47
    We'd rather just use Python and the super mechanism, as we know how
48
    to reason about that. This base class is a hack to make this
49
    possible.
50

51
    The hack requires us to set __bases__, __module__ and
52
    __name__. This fools zope.testing into thinking that this layer
53
    instance is a class it can work with.
54

55
    It'd be better if zope.testing just called a minimal API and
56
    didn't try to be fancy. Fancy layer inheritance mechanisms can
57
    then be implemented elsewhere if people want to. But unfortunately
58
    it does implement a fancy mechanism and we need to fool it.
59
    """
60

61
    __bases__ = ()
1✔
62

63
    def __init__(self, package, name=None):
1✔
64
        if name is None:
1!
65
            name = self.__class__.__name__
1✔
66
        self.__name__ = name
1✔
67
        self.__module__ = package.__name__
1✔
68
        self.package = package
1✔
69

70
    def setUp(self):
1✔
71
        pass
1✔
72

73
    def tearDown(self):
1✔
74
        pass
1✔
75

76
    def testSetUp(self):
1✔
77
        pass
1✔
78

79
    def testTearDown(self):
1✔
80
        pass
1✔
81

82

83
class ZCMLLayerBase(LayerBase):
1✔
84
    """Base class to load up some ZCML.
85
    """
86

87
    def __init__(self, package, name=None, features=None):
1✔
88
        super().__init__(package, name)
1✔
89
        self.features = features or []
1✔
90

91
    def setUp(self):
1✔
92
        setHooks()
1✔
93
        context = config.ConfigurationMachine()
1✔
94
        xmlconfig.registerCommonDirectives(context)
1✔
95
        for feature in self.features:
1✔
96
            context.provideFeature(feature)
1✔
97
        self.context = self._load_zcml(context)
1✔
98
        provideHandler(events.append, (None,))
1✔
99

100
    def testTearDown(self):
1✔
101
        clearEvents()
1✔
102

103
    def tearDown(self):
1✔
104
        cleanUp()
1✔
105

106
    def _load_zcml(self, context):
1✔
107
        raise NotImplementedError
108

109

110
class ZCMLFileLayer(ZCMLLayerBase):
1✔
111
    """This layer can be used to run tests with a ZCML file loaded.
112

113
    The ZCML file is assumed to include sufficient (meta)configuration
114
    so that it can be interpreted itself. I.e. to create a ZCMLLayer
115
    based on another ZCMLLayer's ZCML, just use a ZCML include
116
    statement in your own ZCML to load it.
117
    """
118

119
    def __init__(self, package, zcml_file='ftesting.zcml',
1✔
120
                 name=None, features=None):
121
        super().__init__(package, name, features)
1✔
122
        self.zcml_file = os.path.join(os.path.dirname(package.__file__),
1✔
123
                                      zcml_file)
124

125
    def _load_zcml(self, context):
1✔
126
        return xmlconfig.file(self.zcml_file,
1✔
127
                              package=self.package,
128
                              context=context, execute=True)
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc