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

zopefoundation / zope.component / 3829079989

pending completion
3829079989

push

github

Jens Vagelpohl
- prepare release 5.1.0

467 of 489 branches covered (95.5%)

Branch coverage included in aggregate %.

4620 of 4625 relevant lines covered (99.89%)

1.0 hits per line

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

98.0
/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 xmlconfig, config
1✔
18
try:
1✔
19
    from zope.testing.cleanup import cleanUp
1✔
20
except ImportError: # pragma: no cover
21
    def cleanUp():
22
        pass
23

24
from zope.component import provideHandler
1✔
25
from zope.component.hooks import setHooks
1✔
26
from zope.component.eventtesting import events, clearEvents
1✔
27

28

29
class LayerBase(object):
1✔
30
    """Sane layer base class.
31

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

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

43
    We'd rather just use Python and the super mechanism, as we know how
44
    to reason about that. This base class is a hack to make this
45
    possible.
46

47
    The hack requires us to set __bases__, __module__ and
48
    __name__. This fools zope.testing into thinking that this layer
49
    instance is a class it can work with.
50

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

57
    __bases__ = ()
1✔
58

59
    def __init__(self, package, name=None):
1✔
60
        if name is None:
1!
61
            name = self.__class__.__name__
1✔
62
        self.__name__ = name
1✔
63
        self.__module__ = package.__name__
1✔
64
        self.package = package
1✔
65

66
    def setUp(self):
1✔
67
        pass
1✔
68

69
    def tearDown(self):
1✔
70
        pass
1✔
71

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

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

78
class ZCMLLayerBase(LayerBase):
1✔
79
    """Base class to load up some ZCML.
80
    """
81
    def __init__(self, package, name=None, features=None):
1✔
82
        super(ZCMLLayerBase, self).__init__(package, name)
1✔
83
        self.features = features or []
1✔
84

85
    def setUp(self):
1✔
86
        setHooks()
1✔
87
        context = config.ConfigurationMachine()
1✔
88
        xmlconfig.registerCommonDirectives(context)
1✔
89
        for feature in self.features:
1✔
90
            context.provideFeature(feature)
1✔
91
        self.context = self._load_zcml(context)
1✔
92
        provideHandler(events.append, (None,))
1✔
93

94
    def testTearDown(self):
1✔
95
        clearEvents()
1✔
96

97
    def tearDown(self):
1✔
98
        cleanUp()
1✔
99

100
    def _load_zcml(self, context):
1✔
101
        raise NotImplementedError
102

103
class ZCMLFileLayer(ZCMLLayerBase):
1✔
104
    """This layer can be used to run tests with a ZCML file loaded.
105

106
    The ZCML file is assumed to include sufficient (meta)configuration
107
    so that it can be interpreted itself. I.e. to create a ZCMLLayer
108
    based on another ZCMLLayer's ZCML, just use a ZCML include
109
    statement in your own ZCML to load it.
110
    """
111
    def __init__(self, package, zcml_file='ftesting.zcml',
1✔
112
                 name=None, features=None):
113
        super(ZCMLFileLayer, self).__init__(package, name, features)
1✔
114
        self.zcml_file = os.path.join(os.path.dirname(package.__file__),
1✔
115
                                      zcml_file)
116

117
    def _load_zcml(self, context):
1✔
118
        return xmlconfig.file(self.zcml_file,
1✔
119
                              package=self.package,
120
                              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