Package: lribeiro.cherrypy.test
Test utilities for cherrypy. Can be run under py.test, nosetests or unittest.Based oncode provided by Sylvain HellegouarchThe response returned by the methods on the classlribeiro.cherrypy.test.CPTestCaseis added two properties:status_codeandtext, providing an easier way to retrieve the response body as a string and the status code as
an integerThe classlribeiro.cherrypy.test.CPTestCaseprovides facilities to setup serverless testing and has helper methods
to perform requests.You need to provide arootclass attribute when usingCPTestCasethat will be the root of your test application.You can also provide apreserve_cookiesclass attribute to control whether the cookies must be resent with every
request. This is useful when testing authentication. The default value isTrue.methoddescriptionget()GET requestpost()POST requestput()PUT requestdelete()DELETE requesthead()HEAD requestpatch()PATCH requestThese methods call therequest()method with the specified http method set passing any additional parameter.Therequest()method accepts the following parameters (none of them is required - defaults in parenthesis):parameterdescriptionpathpath to request (‘/’)methodmethod to request (‘GET’)app_pathpath of the application to make requests against (‘’)schemehttp or https (‘http’)protohttp version (‘HTTP/1.1’)datadict contaning data to post on request body (None)headersdict contaning request headers to be sent (None)cookiesdict contaning cookies to be sent (None)auto_redirectwhether to follow redirect responses (False)**kwargsdata to be sent in querystring or request body, in case of POST or PUTIn addition to the request methods, the are assertion methods:methoddescriptionassert_statusassert a specific https status. Constants for http status can be found inlribeiro.cherrypy.test.httpstatusmoduleassert_okshortcut toassert_status(httpstatus.OK)assert_not_foundshortcut toassert_status(httpstatus.NOT_FOUND)assert_errorshortcut toassert_status(httpstatus.INTERNAL_SERVER_ERROR)assert_redirectassert the the response is a redirectassert_redirect_toassert the the response is a redirect and verifies the Location of the redirectassert_not_redirectassert that a response is not a redirectassert_containsassert that the response body contains a stringassert_not_containsassert that the response body does not contain a stringassert_bodyassert that the response body is equals to a stringassert_headerassert that the a response header has a specific valueassert_has_headerassert that the response has a headerassert_cookieassert that the a response cookie has a specific valueassert_has_cookieassert that the response has a cookieassert_pathassert that the request path_info is the same as the one givenThere is also pythonic versions of theassert_methods in theunittest.TestCaseclass, like:CPTestCase.assert_trueCPTestCase.assert_raisesCPTestCase.assert_inand so on.Developed under Python3.4 and tested against Python2.7, Python3.4 and pypy.Example:importcherrypyfromlribeiro.cherrypy.testimportCPTestCaseclassRoot:@cherrypy.exposedefindex(self):return'index'@cherrypy.exposedefpost(self,name):return'name = '+name@cherrypy.exposedefredir(self,to)raisecherrypy.HTTPRedirect(to)classTestTests(CPTestCase):root=Root()deftest_index(self):self.get('/')self.assert_ok()self.assert_not_redirect()deftest_post(self):self.post('/post',name='Lorem Ipsum')self.assert_contains('Lorem Ipsum')deftest_redir(self):self.get('/redir',to='/some/page')self.assert_redirect('/some/page')deftest_pythonic_assertion(self):self.assert_true(True)self.assert_equal('equal','equal')withself.assert_raises(Exception):raiseException()