zope.pytest API

Helpers for py.test integration in Zope-based environments.

The main test helpers are the first two functions (create_app(), configure()) which are normally accessed deploying the py.test funcarg mechanism.

create_app()

zope.pytest.create_app(request, site_root)

Get a WSGI publisher application.

Returns a zope.app.wsgi.WSGIPublisherApplication with site_root stored in the root folder under the name test.

This function is meant to be used as setup/teardown handler deploying a pytest_funcarg__ function.

It sets up a ZODB connection to a ZODB DemoStorage and adds finalizers (tear_down-functionality for people from other testing frameworks) to shutdown the whole setup after the test session.

The request parameter is expected to be a py.test FuncargRequest (not an HTTP request or similar) which is generated by py.test while running tests.

The handle_errors attribute of the returned application is set to False so that errors are handled by the server.

To make use of this setup, you have to configure the environment properly before. Most notably you have to register basic components that subscribe to IDatabaseOpenedEvents, etc. This can normally be done using configure() or setup_config().

See also

pytest_funcarg__ docs on pytest.org.

configure()

zope.pytest.configure(request, module, zcml)

Configure the environment.

Performs a ZCML-driven configuration.

The request parameter
is expected to be a py.test FuncargRequest (not an HTTP request or similar) which is generated by py.test while running tests.
module
is a Python module in which the ZCML file is looked up.
zcml
is a string containing the name of the ZCML file to parse. In many projects this configuration file for tests is called ftesting.zcml.

You normally run this function before calling create_app() as it registers basic components and event subscribers that become active when a ZODB connection is created and opened.

This function is normally called in a pytest_funcarg__ function which will be executed before your test.

It adds cached setup and teardown code to be run before and after your session. That means the configuration setup and teardown will happen only once for a complete test run.

See also

pytest_funcarg__ docs on pytest.org.

setup_config()

zope.pytest.setup_config(package, zcml_file)

Setup a configuration.

Execute the configuration specified by package and zcml_file.

package
is a Python package in which the zcml_file is looked up.
zcml_file
is a string giving the name of a ZCML file to parse.

This is a helper function used by configure(). You normally use configure() to setup tests.

The function registers the most common directives and then tries to parse the and execute the configuration as given in the ZCML file.

Returns the resulting ZCML configuration.

teardown_config()

zope.pytest.teardown_config(config)

Clean up a ZCML configuration.

Unregisters components registered.

setup_db()

zope.pytest.setup_db()

Create a ZODB DB with a demo storage.

Creates a ZODB DemoStorage, turns it into a ZODB DB named main and sends a DatebaseOpened event to inform other components.

You normally have to configure the Zope Component Architechture using setup_config() or (preferably) configure() before you call this function.

Returns a ZODB DB.

teardown_db()

zope.pytest.teardown_db(db)

Unregister ZODB DB.

Unregisters the main database with the global site manager and closes the DB.

setup_connection()

zope.pytest.setup_connection(db)

Open a connection to db.

db
is a ZODB DB as returned from setup_db().

Returns a connection to the database.

teardown_connection()

zope.pytest.teardown_connection(connection)

Shutdown ZODB DB connection.

connection
is a (normally open) ZODB DB connection as returned by setup_connection().

Aborts any running transaction and closes the connection.

setup_root()

zope.pytest.setup_root(connection)

Get the application root.

Returns the Zope application root from a ZODB DB connected to by connection.

connection
is an (already opened) connection to a ZODB DB.

Return the application root, i.e. the object that contains the global site manager and other fundamental ZCA-related stuff.

teardown_root()

zope.pytest.teardown_root(root)

Shutdown the application root.

root
is a ZODB DB application root.

This function does actually nothing.