Test utilities for CherryPy
Project description
===================
cherrypy.authorizer
===================
Test utilities for cherrypy. Can be run under py.test, nosetests or unittest.
Based on `code provided by Sylvain Hellegouarch <https://bitbucket.org/Lawouach/cherrypy-recipes/src/d140e6da973aa271e6b68a8bc187e53615674c5e/testing/unit/serverless/?at=default>`_
The response returned by the methods on the class ``lribeiro.cherrypy.test.CPTestCase`` is added two properties:
``status_code`` and ``text``, providing an easier way to retrieve the response body as a string and the status code as
an integer
The class ``lribeiro.cherrypy.test.CPTestCase`` provides facilities to setup serverless testing and has helper methods
to perform requests.
You need to provide a ``root`` class attribute when using ``CPTestCase`` that will be the root of your test application.
You can also provide a ``preserve_cookies`` class attribute to control whether the cookies must be resent with every
request. This is useful when testing authentication. The default value is ``True``.
======== ==============
method description
======== ==============
get() GET request
post() POST request
put() PUT request
delete() DELETE request
head() HEAD request
patch() PATCH request
======== ==============
These methods call the ``request()`` method with the specified http method set passing any additional parameter.
The ``request()`` method accepts the following parameters (none of them is required - defaults in parenthesis):
============= ======================================================================
parameter description
============= ======================================================================
path path to request ('/')
method method to request ('GET')
app_path path of the application to make requests against ('')
scheme http or https ('http')
proto http version ('HTTP/1.1')
data dict contaning data to post on request body (None)
headers dict contaning request headers to be sent (None)
cookies dict contaning cookies to be sent (None)
auto_redirect whether to follow redirect responses (False)
\*\*kwargs data to be sent in querystring or request body, in case of POST or PUT
============= ======================================================================
In addition to the request methods, the are assertion methods:
=================== ======================================================================================================================
method description
=================== ======================================================================================================================
assert_status assert a specific https status. Constants for http status can be found in ``lribeiro.cherrypy.test.httpstatus`` module
assert_ok shortcut to ``assert_status(httpstatus.OK)``
assert_not_found shortcut to ``assert_status(httpstatus.NOT_FOUND)``
assert_error shortcut to ``assert_status(httpstatus.INTERNAL_SERVER_ERROR)``
assert_redirect assert the the response is a redirect
assert_redirect_to assert the the response is a redirect and verifies the Location of the redirect
assert_not_redirect assert that a response is not a redirect
assert_contains assert that the response body contains a string
assert_not_contains assert that the response body does not contain a string
assert_body assert that the response body is equals to a string
assert_header assert that the a response header has a specific value
assert_has_header assert that the response has a header
assert_cookie assert that the a response cookie has a specific value
assert_has_cookie assert that the response has a cookie
assert_path assert that the request path_info is the same as the one given
=================== ======================================================================================================================
Developed under Python3.4 and tested against Python2.7, Python3.4 and pypy.
Example:
--------
.. sourcecode:: python
import cherrypy
from lribeiro.cherrypy.test import CPTestCase
class Root:
@cherrypy.expose
def index(self):
return 'index'
@cherrypy.expose
def post(self, name):
return 'name = ' + name
@cherrypy.expose
def redir(self, to)
raise cherrypy.HTTPRedirect(to)
class TestTests(CPTestCase):
root = Root()
def test_index(self):
self.get('/')
self.assert_ok()
self.assert_not_redirect()
def test_post(self):
self.post('/post', name='Lorem Ipsum')
self.assert_contains('Lorem Ipsum')
def test_redir(self):
self.get('/redir', to='/some/page')
self.assert_redirect('/some/page')
cherrypy.authorizer
===================
Test utilities for cherrypy. Can be run under py.test, nosetests or unittest.
Based on `code provided by Sylvain Hellegouarch <https://bitbucket.org/Lawouach/cherrypy-recipes/src/d140e6da973aa271e6b68a8bc187e53615674c5e/testing/unit/serverless/?at=default>`_
The response returned by the methods on the class ``lribeiro.cherrypy.test.CPTestCase`` is added two properties:
``status_code`` and ``text``, providing an easier way to retrieve the response body as a string and the status code as
an integer
The class ``lribeiro.cherrypy.test.CPTestCase`` provides facilities to setup serverless testing and has helper methods
to perform requests.
You need to provide a ``root`` class attribute when using ``CPTestCase`` that will be the root of your test application.
You can also provide a ``preserve_cookies`` class attribute to control whether the cookies must be resent with every
request. This is useful when testing authentication. The default value is ``True``.
======== ==============
method description
======== ==============
get() GET request
post() POST request
put() PUT request
delete() DELETE request
head() HEAD request
patch() PATCH request
======== ==============
These methods call the ``request()`` method with the specified http method set passing any additional parameter.
The ``request()`` method accepts the following parameters (none of them is required - defaults in parenthesis):
============= ======================================================================
parameter description
============= ======================================================================
path path to request ('/')
method method to request ('GET')
app_path path of the application to make requests against ('')
scheme http or https ('http')
proto http version ('HTTP/1.1')
data dict contaning data to post on request body (None)
headers dict contaning request headers to be sent (None)
cookies dict contaning cookies to be sent (None)
auto_redirect whether to follow redirect responses (False)
\*\*kwargs data to be sent in querystring or request body, in case of POST or PUT
============= ======================================================================
In addition to the request methods, the are assertion methods:
=================== ======================================================================================================================
method description
=================== ======================================================================================================================
assert_status assert a specific https status. Constants for http status can be found in ``lribeiro.cherrypy.test.httpstatus`` module
assert_ok shortcut to ``assert_status(httpstatus.OK)``
assert_not_found shortcut to ``assert_status(httpstatus.NOT_FOUND)``
assert_error shortcut to ``assert_status(httpstatus.INTERNAL_SERVER_ERROR)``
assert_redirect assert the the response is a redirect
assert_redirect_to assert the the response is a redirect and verifies the Location of the redirect
assert_not_redirect assert that a response is not a redirect
assert_contains assert that the response body contains a string
assert_not_contains assert that the response body does not contain a string
assert_body assert that the response body is equals to a string
assert_header assert that the a response header has a specific value
assert_has_header assert that the response has a header
assert_cookie assert that the a response cookie has a specific value
assert_has_cookie assert that the response has a cookie
assert_path assert that the request path_info is the same as the one given
=================== ======================================================================================================================
Developed under Python3.4 and tested against Python2.7, Python3.4 and pypy.
Example:
--------
.. sourcecode:: python
import cherrypy
from lribeiro.cherrypy.test import CPTestCase
class Root:
@cherrypy.expose
def index(self):
return 'index'
@cherrypy.expose
def post(self, name):
return 'name = ' + name
@cherrypy.expose
def redir(self, to)
raise cherrypy.HTTPRedirect(to)
class TestTests(CPTestCase):
root = Root()
def test_index(self):
self.get('/')
self.assert_ok()
self.assert_not_redirect()
def test_post(self):
self.post('/post', name='Lorem Ipsum')
self.assert_contains('Lorem Ipsum')
def test_redir(self):
self.get('/redir', to='/some/page')
self.assert_redirect('/some/page')
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for lribeiro.cherrypy.test-1.0.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | af73f1c2454008da64f0f317dd7f94b79b42f67fd4266054f0fc0d5a087b9eda |
|
MD5 | bca0effdd544c289da5808959f3ec00c |
|
BLAKE2b-256 | 882a968858d3d74c0783c7781c088a6b61f2c73f47d5b1f714b3c9d098c9eedc |
Close
Hashes for lribeiro.cherrypy.test-1.0.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5b6e36270398eb6cd1cb4488bd8ddb36495037f45399cd2715fc3efdea605cba |
|
MD5 | 1b1a3c843a10d9895898f14ab1bebbd7 |
|
BLAKE2b-256 | 86f0aac88ffe4cd0d652727bbcf86dffd605c61ffdd5f16d65b6e3da83b6fe5b |