Introduction¶
zope.pytest
contains a set of helper functions to test
Zope/Grok using pytest.
Use pytest for your Zope/Grok tests¶
Many Zope/Grok applications today are tested using regular Python
unittest
and/or the respective zope packages, testrunners, etc.
Many Python developers, however, prefer the more pythonic way of pytest to detect, execute and evaluate tests.
zope.pytest
now brings the power and beauty of pytest into
Zope/Grok projects.
Quickstart¶
Zope applications normally have to setup a ZODB database and
configure the Zope component architecture to run. At least
integration and functional tests therefore have to perform a lot of
setup/teardown functionality. That’s where zope.pytest
comes to
rescue.
With zope.pytest
you can define pytest-compatible
setup/teardown code simply like this:
import mypkg
from mypkg.app import MyApp
from zope.pytest import configure, create_app
def pytest_funcarg__config(request):
return configure(request, mypkg, 'ftesting.zcml')
def pytest_funcarg__app(request):
return create_app(request, MyApp())
def test_app(config, app):
assert 1 is 1
This setup requires that you provide a valid configuration in an
ftesting.zcml
file in your package.
Activating pytest and zope.pytest
in your project¶
In the buildout.cfg
of your project simply add pytest
as a
requirement to build tests. This can for instance be done like this:
[buildout]
develop = .
parts = test
versions = versions
[versions]
[test]
recipe = zc.recipe.egg
eggs =
mypkg [tests]
pytest
The test
extra requirement mentioned in the [test]
section can
be setup like this in your project’s setup.py
:
tests_require = [
'pytest',
'zope.app.appsetup',
'zope.pytest',
]
setuptools.setup(
# ...
extras_require = {
'tests': tests_require,
# ...
}
)
That’s it. Run buildout and a py.test script should be created in
your bin/
directory. Now you can go on and write your tests.