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

zopefoundation / zope.component / 16248888290

25 Jun 2025 07:38AM UTC coverage: 99.774%. Remained the same
16248888290

push

github

web-flow
Merge pull request #77 from adrian-zon/patch-1

Fix typo

241 of 252 branches covered (95.63%)

Branch coverage included in aggregate %.

4615 of 4615 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 ModuleNotFoundError:  # pragma: no cover
24

25
    def cleanUp():
26
        pass
27

28

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

34

35
class LayerBase:
1✔
36
    """Sane layer base class.
37

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

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

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

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

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

63
    __bases__ = ()
1✔
64

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

72
    def setUp(self):
1✔
73
        pass
1✔
74

75
    def tearDown(self):
1✔
76
        pass
1✔
77

78
    def testSetUp(self):
1✔
79
        pass
1✔
80

81
    def testTearDown(self):
1✔
82
        pass
1✔
83

84

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

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

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

102
    def testTearDown(self):
1✔
103
        clearEvents()
1✔
104

105
    def tearDown(self):
1✔
106
        cleanUp()
1✔
107

108
    def _load_zcml(self, context):
1✔
109
        raise NotImplementedError
110

111

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

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

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

130
    def _load_zcml(self, context):
1✔
131
        return xmlconfig.file(self.zcml_file,
1✔
132
                              package=self.package,
133
                              context=context,
134
                              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