[Zope3-checkins] SVN: zope.testing/trunk/src/zope/testing/ Added a
small framework for automating doctest setUp/tearDown.
Jim Fulton
jim at zope.com
Thu Mar 29 14:40:46 EDT 2007
Log message for revision 73918:
Added a small framework for automating doctest setUp/tearDown.
Changed:
A zope.testing/trunk/src/zope/testing/setupstack.py
A zope.testing/trunk/src/zope/testing/setupstack.txt
U zope.testing/trunk/src/zope/testing/tests.py
-=-
Added: zope.testing/trunk/src/zope/testing/setupstack.py
===================================================================
--- zope.testing/trunk/src/zope/testing/setupstack.py 2007-03-29 16:05:34 UTC (rev 73917)
+++ zope.testing/trunk/src/zope/testing/setupstack.py 2007-03-29 18:40:46 UTC (rev 73918)
@@ -0,0 +1,42 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Stack-based test doctest setUp and tearDown
+
+See setupstack.txt
+"""
+
+import os, shutil, tempfile
+
+key = '__' + __name__
+
+def register(test, function, *args, **kw):
+ stack = test.globs.get(key)
+ if stack is None:
+ stack = test.globs[key] = []
+ stack.append((function, args, kw))
+
+def tearDown(test):
+ stack = test.globs.get(key)
+ while stack:
+ f, p, k = stack.pop(0)
+ f(*p, **k)
+
+def setUpDirectory(test):
+ tmp = tempfile.mkdtemp()
+ register(test, shutil.rmtree, tmp)
+ here = os.getcwd()
+ register(test, os.chdir, here)
+ os.chdir(tmp)
+
+
Property changes on: zope.testing/trunk/src/zope/testing/setupstack.py
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Added: zope.testing/trunk/src/zope/testing/setupstack.txt
===================================================================
--- zope.testing/trunk/src/zope/testing/setupstack.txt 2007-03-29 16:05:34 UTC (rev 73917)
+++ zope.testing/trunk/src/zope/testing/setupstack.txt 2007-03-29 18:40:46 UTC (rev 73918)
@@ -0,0 +1,94 @@
+Stack-based test doctest setUp and tearDown
+============================================
+
+Writing doctest setUp and tearDown functions can be a bit tedious,
+especially when setUp/tearDown functions are combined.
+
+the zope.testing.setupstack module provides a small framework for
+automating test tear down. It provides a generic setUp function that
+sets up a stack. Normal test setUp functions call this function to set
+up the stack and then use the register function to register tear-down
+functions.
+
+To see how this works we'll create a faux test:
+
+ >>> class Test:
+ ... def __init__(self):
+ ... self.globs = {}
+ >>> test = Test()
+
+We'll register some tearDown functions that just print something:
+
+ >>> import sys
+ >>> import zope.testing.setupstack
+ >>> zope.testing.setupstack.register(
+ ... test, lambda : sys.stdout.write('td 1\n'))
+ >>> zope.testing.setupstack.register(
+ ... test, lambda : sys.stdout.write('td 2\n'))
+
+Now, when we call the tearDown function:
+
+ >>> zope.testing.setupstack.tearDown(test)
+ td 1
+ td 2
+
+The registered tearDown functions are run.
+
+Extra positional arguments can be passed to register:
+
+ >>> zope.testing.setupstack.register(
+ ... test, lambda x, y, z: sys.stdout.write('%s %s %s\n' % (x, y, z)),
+ ... 1, 2, z=9)
+ >>> zope.testing.setupstack.tearDown(test)
+ 1 2 9
+
+
+Temporary Test Directory
+------------------------
+
+Often, tests create files as they demonstrate functionality. They
+need to arrange for the removeal of these files when the test is
+cleaned up.
+
+The setUpDirectory function automates this. We'll get the current
+directory first:
+
+ >>> import os
+ >>> here = os.getcwd()
+
+We'll also create a new test:
+
+ >>> test = Test()
+
+Now we'll call the setUpDirectory function:
+
+ >>> zope.testing.setupstack.setUpDirectory(test)
+
+We don't have to call zope.testing.setupstack.setUp, because
+setUpDirectory calls it for us.
+
+Now the current working directory has changed:
+
+ >>> here == os.getcwd()
+ False
+
+We can create files to out heart's content:
+
+ >>> open('Data.fs', 'w').write('xxx')
+ >>> os.path.exists('Data.fs')
+ True
+
+When tearDown is called:
+
+ >>> zope.testing.setupstack.tearDown(test)
+
+We'll be back where we started:
+
+ >>> here == os.getcwd()
+ True
+
+and the files we created will be gone (along with the temporary
+directory that was created:
+
+ >>> os.path.exists('Data.fs')
+ False
Property changes on: zope.testing/trunk/src/zope/testing/setupstack.txt
___________________________________________________________________
Name: svn:eol-style
+ native
Modified: zope.testing/trunk/src/zope/testing/tests.py
===================================================================
--- zope.testing/trunk/src/zope/testing/tests.py 2007-03-29 16:05:34 UTC (rev 73917)
+++ zope.testing/trunk/src/zope/testing/tests.py 2007-03-29 18:40:46 UTC (rev 73918)
@@ -27,6 +27,7 @@
doctest.DocTestSuite('zope.testing.loggingsupport'),
doctest.DocTestSuite('zope.testing.server'),
testrunner.test_suite(),
+ doctest.DocFileSuite('setupstack.txt'),
))
if __name__ == '__main__':
More information about the Zope3-Checkins
mailing list