Zope Application Testing Support
Project description
This package provides testing support for Zope 3 applications. Besides providing numerous setup convenience functions, it implements a testing setup that allows the user to make calls to the publisher allowing to write functional tests.
Detailed Dcoumentation
FDocTest (How-To)
Steps to get started:
Use a clean/missing Data.fs
Create a manager with the name “mgr”, password “mgrpw”, and grant the zope.Manager role.
Install tcpwatch.
Create a temporary directory to record tcpwatch output.
Run tcpwatch using: tcpwatch.py -L 8081:8080 -s -r tmpdir (the ports are the listening port and forwarded-to port; the second need to match the Zope configuration)
In a browser, connect to the listening port and do whatever needs to be recorded.
Shut down tcpwatch.
Run the script src/zope/app/testing/dochttp.py: python2.4 src/zope/app/testing/dochttp.py tmpdir > somefile.txt
Edit the generated text file to add explanations and elide uninteresting portions of the output.
In a functional test module (usually ftests.py), import FunctionalDocFileSuite from zope.app.testing.functional and instantiate it, passing the name of the text file containing the test.
DocTest Functional Tests
This file documents and tests doctest-based functional tests and basic Zope web-application functionality.
Request/Response Functional Tests
You can create Functional tests as doctests. Typically, this is done by using a script such as src/zope/app/testing/dochttp.py to convert tcpwatch recorded output to a doctest, which is then edited to provide explanation and to remove uninteresting details. That is how this file was created.
Here we’ll test some of the most basic types of access.
First, we’ll test accessing a protected page without credentials:
>>> print http(r""" ... GET /@@contents.html HTTP/1.1 ... """) HTTP/1.1 401 Unauthorized Cache-Control: no-store, no-cache, must-revalidate Content-Length: ... Content-Type: text/html;charset=utf-8 Expires: Mon, 26 Jul 1997 05:00:00 GMT Pragma: no-cache WWW-Authenticate: basic realm="Zope" <BLANKLINE> <!DOCTYPE html PUBLIC ...
Here we see that we got:
A 401 response,
A WWW-Authenticate header, and
An html body with an error message
Some technical headers to keep squid happy
Note that we used ellipsis to indicate ininteresting details.
Next, we’ll access the same page with credentials:
>>> print http(r""" ... GET /@@contents.html HTTP/1.1 ... Authorization: Basic mgr:mgrpw ... """) HTTP/1.1 200 OK Content-Length: ... Content-Type: text/html;charset=utf-8 <BLANKLINE> <!DOCTYPE html PUBLIC ...
Important note: you must use the user named “mgr” with a password “mgrpw”.
And we get a normal output.
Next we’ll try accessing site management. Since we used “/manage”, we got redirected:
>>> print http(r""" ... GET /++etc++site/@@manage HTTP/1.1 ... Authorization: Basic mgr:mgrpw ... Referer: http://localhost:8081/ ... """) HTTP/1.1 303 See Other Content-Length: 0 Content-Type: text/plain;charset=utf-8 Location: @@contents.html <BLANKLINE>
Note that, in this case, we got a 303 response. A 303 response is the prefered response for this sort of redirect with HTTP 1.1. If we used HTTP 1.0, we’d get a 302 response:
>>> print http(r""" ... GET /++etc++site/@@manage HTTP/1.0 ... Authorization: Basic mgr:mgrpw ... Referer: http://localhost:8081/ ... """) HTTP/1.0 302 Moved Temporarily Content-Length: 0 Content-Type: text/plain;charset=utf-8 Location: @@contents.html <BLANKLINE>
Lets visit the page we were redirected to:
>>> print http(r""" ... GET /++etc++site/@@contents.html HTTP/1.1 ... Authorization: Basic mgr:mgrpw ... Referer: http://localhost:8081/ ... """) HTTP/1.1 200 OK Content-Length: ... Content-Type: text/html;charset=utf-8 <BLANKLINE> <!DOCTYPE html PUBLIC ...
Finally, lets access the default page for the site:
>>> print http(r""" ... GET / HTTP/1.1 ... Authorization: Basic mgr:mgrpw ... """) HTTP/1.1 200 OK Content-Length: ... Content-Type: text/html;charset=utf-8 <BLANKLINE> <!DOCTYPE html PUBLIC ...
Access to the object system
You can use the getRootFolder() function:
>>> root = getRootFolder() >>> root <zope.app.folder.folder.Folder object at ...>
You can intermix HTTP requests with regular Python calls. Note, however, that making an http() call implied a transaction commit. If you want to throw away changes made in Python code, abort the transaction before the HTTP request.
>>> print http(r""" ... POST /@@contents.html HTTP/1.1 ... Authorization: Basic mgr:mgrpw ... Content-Length: 73 ... Content-Type: application/x-www-form-urlencoded ... ... type_name=BrowserAdd__zope.app.folder.folder.Folder&new_value=f1""") HTTP/1.1 303 See Other Content-Length: ... Content-Type: text/html;charset=utf-8 Location: http://localhost/@@contents.html <BLANKLINE> <!DOCTYPE html ...
Now we can see that the new folder was added:
>>> list(root.keys()) [u'f1']
CHANGES
3.5.0 (2008-08-20)
Add support for product-configuration as part of functional layers; this more closely mirrors the configuration order for normal operation.
3.4.3 (2008-07-25)
Fix memory leak in all functional tests. see: https://bugs.launchpad.net/zope3/+bug/251273
3.4.2 (2008-02-02)
Fix of 599 error on conflict error in request see: http://mail.zope.org/pipermail/zope-dev/2008-January/030844.html
3.4.1 (2007-10-31)
Fixed deprecation warning for ZopeSecurityPolicy.
3.4.0 (2007-10-27)
Initial release independent of the main Zope tree.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Hashes for zope.app.testing-3.5.1dev.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4e19dfb1c9c3a41196980c791e43feb5e85e7a2d927883cd2eb5adfba366883 |
|
MD5 | d851260e637982b67159b0325a59042e |
|
BLAKE2b-256 | a48684c9c4028cf8201a343d98cbd2b295eaa4391ea85cd3d998ad534f5290dd |