[Checkins] SVN: bluebream/website/docs/v1.0/howto/ new HOWTO

Baiju M baiju.m.mail at gmail.com
Fri Mar 5 23:45:21 EST 2010


Log message for revision 109696:
  new HOWTO
  

Changed:
  U   bluebream/website/docs/v1.0/howto/index.rst
  A   bluebream/website/docs/v1.0/howto/testingpersistent.rst

-=-
Modified: bluebream/website/docs/v1.0/howto/index.rst
===================================================================
--- bluebream/website/docs/v1.0/howto/index.rst	2010-03-06 03:45:36 UTC (rev 109695)
+++ bluebream/website/docs/v1.0/howto/index.rst	2010-03-06 04:45:21 UTC (rev 109696)
@@ -15,6 +15,7 @@
    newdependency
    absoluteurl
    dynamicfields
+   testingpersistent
 
 .. raw:: html
 

Added: bluebream/website/docs/v1.0/howto/testingpersistent.rst
===================================================================
--- bluebream/website/docs/v1.0/howto/testingpersistent.rst	                        (rev 0)
+++ bluebream/website/docs/v1.0/howto/testingpersistent.rst	2010-03-06 04:45:21 UTC (rev 109696)
@@ -0,0 +1,88 @@
+Testing persistent objects
+==========================
+
+.. based on http://www.mail-archive.com/zope3-users@zope.org/msg03555.html
+
+You can create a fake module to test persistent objects with
+transaction commits, otherwise you will get an error like this::
+
+  PicklingError: Can't pickle <class 'TestItem'>: attribute lookup __builtin__.TestItem failed
+
+This HOWTO explain creating fake module to write test cases.  The
+functionality to create fake module is provided by
+``zope.testing.module``.
+
+Inside your ticket collector application, you can create a
+``persistent_test.txt`` file with a test case like this::
+
+  :doctest:
+
+  >>> from ZODB.tests.util import DB
+  >>> import transaction
+  >>> db = DB()
+  >>> conn = db.open()
+  >>> root = conn.root()
+
+  >>> from persistent import Persistent
+
+  >>> class TestItem(Persistent):
+  ...     pass
+
+  >>> item = TestItem()
+  >>> root['item'] = item
+  >>> transaction.commit()
+
+Now invoke the test runner as given below::
+
+  $ ./bin/test
+
+You should get an erorr like this::
+
+  PicklingError: Can't pickle <class 'TestItem'>: attribute lookup __builtin__.TestItem failed
+
+Now open the ``tests.py`` and add two keyword arguments to
+``z3c.testsetup.register_all_tests`` function named ``setup`` and
+``teardown``.  The values of those keyword arguments could be
+``module.setUp`` and ``module.tearDown`` respectively as given here::
+
+  test_suite = z3c.testsetup.register_all_tests('testp2.main', setup=module.setUp, teardown=module.tearDown)
+
+A better method would be to create your own ``setUp`` and
+``tearDown`` functions as given below so that you can add more code
+there, if required.  Here is custom ``setUp`` and ``tearDown``
+without any extra code::
+
+  import z3c.testsetup
+  from zope.testing import module
+
+  def setUp(test):
+      module.setUp(test)
+
+  def tearDown(test):
+      module.tearDown(test)
+
+  test_suite = z3c.testsetup.register_all_tests('testp2.main', setup=setUp, teardown=tearDown)
+
+
+By default the fake module name will be ``__main__``.  If you require
+another name for the module, you can specify it like this::
+
+   module.setUp(test, 'mymodule')
+   module.tearDown(test, 'mymodule')
+
+Now you can import the ``mymodule`` from the test cases
+(``persistent_test.txt``).  Based on this module name functionality,
+here is a better way to create ``setUp`` and ``tearDown``::
+
+  import z3c.testsetup
+  from zope.testing import module
+
+  module_name = 'testp2.main.mypersistent'
+
+  def setUp(test):
+      module.setUp(test, module_name)
+
+  def tearDown(test):
+      module.tearDown(test, module_name)
+
+  test_suite = z3c.testsetup.register_all_tests('testp2.main', setup=setUp, teardown=tearDown)



More information about the checkins mailing list