~launchpad/zope.testing/3.9.4-p4

« back to all changes in this revision

Viewing changes to src/zope/testing/cleanup.py

  • Committer: Maris Fogels
  • Date: 2010-06-04 14:58:44 UTC
  • Revision ID: maris.fogels@canonical.com-20100604145844-mb48c1ig9gty2kao
Initial import of zope.testing's 3.9.4 SVN tag

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2001, 2002 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
"""Provide a standard cleanup registry
 
15
 
 
16
Unit tests that change global data should include the CleanUp base
 
17
class, which provides simpler setUp and tearDown methods that call
 
18
global-data cleanup routines::
 
19
 
 
20
  class Test(CleanUp, unittest.TestCase):
 
21
 
 
22
      ....
 
23
 
 
24
If custom setUp or tearDown are needed, then the base routines should
 
25
be called, as in::
 
26
 
 
27
  def tearDown(self):
 
28
      super(Test, self).tearDown()
 
29
      ....
 
30
 
 
31
Cleanup routines for global data should be registered by passing them to
 
32
addCleanup::
 
33
 
 
34
 
 
35
  addCleanUp(pigRegistry._clear)
 
36
 
 
37
 
 
38
$Id: cleanup.py 110538 2010-04-06 03:02:54Z tseaver $
 
39
"""
 
40
_cleanups = []
 
41
 
 
42
def addCleanUp(func, args=(), kw={}):
 
43
    """Register a cleanup routines
 
44
 
 
45
    Pass a function to be called to cleanup global data.
 
46
    Optional argument tuple and keyword arguments may be passed.
 
47
    """
 
48
    _cleanups.append((func, args, kw))
 
49
 
 
50
class CleanUp(object):
 
51
    """Mix-in class providing clean-up setUp and tearDown routines."""
 
52
 
 
53
    def cleanUp(self):
 
54
        """Clean up global data."""
 
55
        cleanUp()
 
56
 
 
57
    setUp = tearDown = cleanUp
 
58
 
 
59
 
 
60
def cleanUp():
 
61
    """Clean up global data."""
 
62
    for func, args, kw in _cleanups:
 
63
        func(*args, **kw)
 
64
 
 
65
setUp = tearDown = cleanUp