How to install ftw.builder
- Download and install ActivePython
- Open Command Prompt
pypm install ftw.builder
Create Plone objects in tests with the Builder Pattern.
The builder pattern simplifies constructing objects. In tests we often need to create Plone objects, sometimes a single object, sometimes a whole graph of objects. Using the builder pattern allows us to do this in a DRY way, so that we do not repeat this over and over.
Add ftw.builder as (test-) dependency to your package in setup.py:
Setup builder session in your testcase
In plone projects you can use the BUILDER_LAYER which your testing layer should base on. So the the session management is handled by the BUILDER_LAYER:
Use the builder for creating objects in your tests:
The BuilderSession keeps configuration for multiple builders. It is set up and destroyed by the BUILDER_LAYER and can be configured or replaced by a custom session with set_builder_session_factory.
When having a functional testing layer (plone.app.testing.FunctionalTesting) and doing browser tests it is necessary that the new objects are committed in the ZODB. When using a IntegrationTesting on the other hand it is essential that nothing is comitted, since this would break test isolation.
The session provides the auto_commit option (dislabed by default), which commits to the ZODB after creating an object. Since it is disabled by default you need to enable it in functional test cases.
A default session factory functional_session_factory that enables the auto-commit feature is provided:
You can use set_builder_session_factory to replace the default session factory in functional tests. Make sure to also base your fixture on the BUILDER_LAYER fixture:
Plone object builders
For creating Plone objects (Archetypes or Dexterity) there are some methods for setting basic options:
- within(container) - tell the builder where to create the object
- titled(title) - name the object
- having(field=value) - set the value of any field on the object
- in_state(review_state) - set the object into any review state of the workflow configured for this type
The ftw.builder ships with some builders for some default Plone (Archetypes) content types, but the idea is that you can easily craft your own builders for your types or extend existing builders.
The built-in builders are:
- folder - creates an Archetypes folder
- page (or Document) - creates an Archetypes page (alias Document)
- file - creates a File
The default Archetypes file builder let's you attach a file or create the file with dummy content:
Creating new builders
The idea is that you create your own builders for your application. This might be builders creating a single Plone object (Archetypes or Dexterity) or builders creating a set of objects using other builders.
Define a simpe builder class for your python object and register them in the builder registry
Use the ArchetypesBuilder base class for creating new Archetypes builders. Set the portal_type and your own methods.
Use the DexterityBuilder base class for creating new Dexterity builders. Set the portal_type and your own methods.
You can do things before and after creating the object:
Sometimes it is necessary to override an existing builder. For re-registering an existing builder you can use the force flag:
Development / Tests
This package is copyright by 4teamwork.
ftw.builder is licensed under GNU General Public License, version 2.
- Added dexterity support. [phgross]
- Initial implementation [jone]