Note that this blog is discontinued. You can find my new blog here: Daniel Nouri's Blog.
For those of you who say they don't like doctests because they don't give you isolation between tests. Here's an example of an integration doctest for Plone that gives you exactly that:
from Testing import ZopeTestCase as ztc
from Products.PloneTestCase import PloneTestCase as ptc
from Products.PloneTestCase.layer import PloneSite
ZOPE_DEPS = ['MyZopeProductDependecy']
PLONE_DEPS = ['MyPloneProduct',
'MyPloneDependency']
for x in ZOPE_DEPS + PLONE_DEPS:
ztc.installProduct(x)
ptc.setupPloneSite(products=PLONE_DEPS)
class MyTest:
def test_one():
r"""
Check if one and one is two:
>>> 1 + 1
2
"""
def test_two():
r"""
Check if one minus one is zero:
>>> 1 - 1
0
"""
def test_suite():
suite = ztc.ZopeDocTestSuite(test_class=ptc.PloneTestCase)
suite.layer = PloneSite
return suite
Look at G. Writing Tests in the (somewhat out of date) The Zope 3 Developer's Book for more inspiration.
Don't do traditional-style unit tests, they're ugly. Is this a matter of test, err taste? I don't think so.
Of course, if you want real isolation, you go for unit tests (i.e. no ZopeTestCase involved). People are usually scared and say, but then I need to set so much up by hand, or, don't make me write a mock object for everything, but those arguments aren't really valid most of the time. This is how easy a pure unit test using doctest for a Plone Product (or any Python program) can look like:
import unittest
from zope.testing import doctest
class Add:
r"""
>>> 1 + 1
2
"""
class Subtract:
r"""
>>> 1 - 1
0
"""
def test_suite():
return unittest.TestSuite((doctest.DocTestSuite()))
If you have a hard time creating mock objects to test against, try Mocky. This is an example of mocking a CMF site that gives you getPhysicalPath, has a portal_catalog and some special site_properties for use with your content type:
>>> from mocky import Mocky
>>> import Acquisition
>>> class MockySite(Mocky, Acquisition.Explicit):
... def getPhysicalPath(self):
... return tuple(self.name.split('.'))
... def portal_catalog(self, **kwargs):
... return []
>>> site = MockySite('site')
>>> props = site.portal_properties.site_properties
>>> props.getProperty = lambda x: 'utf-8' # doctest: +ELLIPSIS
Set site.portal_properties.site_properties.getProperty to <function ...>
>>> from Products.MyProduct.content import MyContentType
>>> content = MyContentType('someid').__of__(site)
>>> content.something_that_involves_using_portalcatalog_and_siteproperties()
'your result here'
Update: The python-in-testing list has an interesting discussion about doctest vs. unittest.
posted at: 16:06 |
0 comments |
category: /devel/zope
|
permanent link |
add to del.icio.us or digg it
I never leave home without these, at least not when I leave home to do development:
Emacs of course isn't specific to Zope or Python. Nevertheless, Emacs is the editor for doing Zope development. Check out Philipp von Weitershausen demo pdbtrack. You need nothing more than the usual python-mode for this. (If you don't know what pdb is, read this gentle intro to debugging in Python.)
I hear that other editors can do this too, but it can't possibly work as well. :-P
Start with Emacs basics right now!
You've certainly been living under a rock if you haven't heard of Firebug before. Never was trying out and debugging JavaScript so joyful. Firebug has an interactive JavaScript shell that since version 1.0 also has access to the variables in the scope of any breakpoint that you might have set.
What it does: It loads the Python Debugger (pdb) whenever Zope encounters an exception that was not filtered in the error_log. In combination with pdbtrack this means: Don't read tracebacks to find out where the exception has occurred and where you might want to hook in to debug. And don't restart after you figured out where to put that pdb.set_trace(). Instead: Have your pdb point to the failing line of code and do your debugging right away. Works similar to --debug-inplace of the Zope testrunner.
This might be not as useful for development of new applications (think tests) as for situations where you have to debug something quickly, possibly on a live deployment.
Before, I totally hated debugging code in Script (Python) objects. That was until I found Lennart Regebro's ScriptDebugging, which helps pdbtrack find out where in the script you are, which means that you can debug scripts just like any other Python code.
Stuck with a Zope that's stuck? If your Zope stops responding, use DeadlockDebugger to find out where exactly Zope's threads are deadlocked. From the author:
This product adds a hook so that a deadlocked Zope process can be debugged, by dumping a traceback of all running python processes. The dump is sent to the event log (at the DEBUG level) and returned to the browser (even though the Zope is deadlocked and doesn't answer any other requests!).
DeadlockDebugger can of course also be used to debug Zope in non-deadlock situations, when a Zope process is taking a long time and you wish to know what code is being executed.
What's the Zope 2 tool that you'd take with you on an island?
posted at: 19:34 |
3 comments |
category: /devel/zope
|
permanent link |
add to del.icio.us or digg it
Luke Slater's party on Second Life is starting now in the Alpha Box. He'll be playing his DJ set in less than an hour!
posted at: 22:03 |
1 comments |
category: /music
|
permanent link |
add to del.icio.us or digg it
| < | March 2007 | > | ||||
| Su | Mo | Tu | We | Th | Fr | Sa |
| 1 | 2 | 3 | ||||
| 4 | 5 | 6 | 7 | 8 | 9 | 10 |
| 11 | 12 | 13 | 14 | 15 | 16 | 17 |
| 18 | 19 | 20 | 21 | 22 | 23 | 24 |
| 25 | 26 | 27 | 28 | 29 | 30 | 31 |
Feed of all categories:
rss |
atom