This package provides useful helper methods for testing in Plone.
The hexagonit.testing.browser module provides an enhanced zope.testbrowser test browser.
The main purpose is to make functional browser tests read easier by reducing the amount of confusing setup code.
To use the enhanced test browser simple instantiate it in place of plone.testing.z2.Browser in your tests, e.g.
from hexagonit.testing.browser import Browser
browser = Browser()
# Set the base URL for easier browsing.
browser.setBaseUrl(self.portal.absolute_url())
# Access objects using readable URLs.
browser.open('/my/cool-page')
browser.open('/some/other/place')
# Login in to the portal
browser.login('r00t', 's3kr3t')
# Inspect the current URL in a real browser.
browser.openBrowser()
# POST data directly without a <form>, including a file upload.
browser.post('/web-api', {
'foo': 'bar',
'my_file_field': {
'filename': 'my-document.pdf',
'content-type': 'application/pdf',
'data' : open('/tmp/my-document.pdf'),
}
})
The hexagonit.testing.date module provides helpers to mock out the dynamic constructors datetime.date.today() and datetime.datetime.now() by providing class generators which produce instances of these classes that return static values.
This is particularly useful when used in conjunction with the mock package during unit testing, e.g.
from hexagonit.testing.date import static_date
from hexagonit.testing.date import static_datetime
import datetime
import mock
import unittest
class MyTestCase(unittest.TestCase):
@mock.patch('my.module.datetime', static_datetime(datetime.datetime(2011, 12, 14, 12, 45)))
@mock.patch('my.module.date', static_date(datetime.date(2011, 12, 14)))
def test_something_that_uses_datetime(self):
# Within the test your application code calling `datetime.now()` or `date.today()`
# will always return the static values.
pass
Enhanced test browser.
Sets a base URL for all subsequent requests.
Usually the base URL is set to portal_url at the beginning of the test with main benefit that subsequent calls to browser.open are easier to read. So instead of writing:
>>> browser.open('{0}/path/to/object'.format(self.portal.absolute_url()))
>>> browser.open('{0}/path/to/somewhere/else'.format(self.portal.absolute_url()))
You can write:
>>> browser.setBaseUrl(self.portal.absolute_url())
>>> browser.open('/path/to/object')
>>> browser.open('/path/to/somewhere/else')
Parameters: | base_url (str) – Base URL to use in subsequent calls to open. |
---|
Dumps the browser contents to the given file.
Parameters: | filename (str) – File name. |
---|
Opens the given URL in the test browser with additional support for base URLs (set using hexagonit.testing.browser.Browser.setBaseUrl()).
The base URL is used when the given URL is an absolute path.
Parameters: |
|
---|
Sets a request header possibly overwriting an existing header with the same name.
Parameters: |
|
---|
Logs into the portal.
Assumes that the browser has been configured with a base URL pointing to the portal root (see hexagonit.testing.browser.Browser.setBaseUrl()).
Parameters: |
|
---|
Deletes a portlet manager of the given name.
Usually it is u’plone.leftcolumn’ or u’plone.rightcolumn’.
Parameters: |
|
---|
Dumps self.browser.contents (HTML) to a file and opens it with a normal browser.
If browser is not specified, system default browser will be used.
Parameters: |
|
---|
Posts the given data to the given url with a multipart/form-data submission instead of application/x-www-form-urlencoded.
This is particularly useful if you need to test web APIs which expect POST request without having to generate forms just for testing purposes.
>>> browser.post('/web-api', {
... 'foo': 'bar',
... 'bar': 'foo',
... })
To POST a file you can use the following dictionary structure as value for the field:
{ 'filename': 'my-document.pdf',
'content-type': 'application/pdf',
'data' : open('/tmp/my-document.pdf'),
}
The dictionary must contain the above fields where filename and content-type are strings and data is a file-like object. To perform a file upload you could make a following kind of call:
>>> browser.post('/web-api', {
... 'my_file_field': {
... 'filename': 'my-document.pdf',
... 'content-type': 'application/pdf',
... 'data' : open('/tmp/my-document.pdf'),
... }
... })
If the order of the submitted fields is significant a sequence of (key, value) tuples may be used instead.
>>> browser.post('/web-api', [
... ('foo', 'bar'),
... ('bar', 'foo'),
... ])
Parameters: |
|
---|
Grepping contents with given regex.
Searching contents with given regex.
Showing only parts specified by given CSS selector
Generates a modified datetime.date class with a static .today() method which returns the given date, which must be an instance of datetime.date.
Parameters: | date (datetime.date) – The static date that the today() will return. |
---|---|
Return type: | Modified datetime.date |
Generates a modified datetime.datetime class with a static .now() method which returns the given date which must be an instance of datetime.datetime.
Parameters: | dt (datetime.datetime) – The static datetime that the now() method will return. |
---|---|
Return type: | Modified datetime.datetime |
Contents: