Python - CherryPy testing - set session data? -


when running pytest unit test against cherrypy server, using cherrypy.helper.cpwebcase sub-class, how can set data session object? tried calling cherrypy.session['foo']='bar' if in cherrypy call, gave "attributeerror: '_serving' object has no attribute 'session'"

for reference, test case might (pulled the cherrypy docs minor edits):

import cherrypy cherrypy.test import helper myapp import root  class simplecptest(helper.cpwebcase):     def setup_server():         cherrypy.tree.mount(root(), "/", {'/': {'tools.sessions.on': true}})      setup_server = staticmethod(setup_server)      def check_two_plus_two_equals_four(self):         #<code set session variable 2 here>         # question: how set session variable?         self.getpage("/")         self.assertstatus('200 ok')         self.assertheader('content-type', 'text/html;charset=utf-8')         self.assertbody('4') 

and handler might (or else, makes no difference whatsoever):

class root:     @cherrypy.expose     def test_handler(self):         #get random session variable ,         number_var=cherrypy.session.get('number')         # add two. fail if session variable has not been set,         # or not number         number_var = number_var+2         return str(number_var) 

it's safe assume config correct, , sessions work expected.

i could, of course, write cherrypy page takes key , value arguments, , sets specified session value, , call test code (edit: i've tested this, , work). that, however, seems kludgy, , i'd want limit testing somehow if went down road.

what trying achieve referred mocking.

while running tests you'd want 'mock' of resources access dummy objects having same interface (duck typing). may achieved monkey patching. simplify process may use unittest.mock.patch either context manager or method/function decorator.

please find below working example context manager option:

==> myapp.py <==

import cherrypy   class root:     _cp_config = {'tools.sessions.on': true}      @cherrypy.expose     def test_handler(self):         # random session variable ,         number_var = cherrypy.session.get('number')         # add two. fail if session variable has not been set,         # or not number         number_var = number_var + 2         return str(number_var) 

==> cp_test.py <==

from unittest.mock import patch  import cherrypy cherrypy.test import helper cherrypy.lib.sessions import ramsession  myapp import root   class simplecptest(helper.cpwebcase):     @staticmethod     def setup_server():         cherrypy.tree.mount(root(), '/', {})      def test_check_two_plus_two_equals_four(self):         # <code set session variable 2 here>         sess_mock = ramsession()         sess_mock['number'] = 2         patch('cherrypy.session', sess_mock, create=true):             # inside of block manipulations `cherrypy.session`             # access `sess_mock` instance instead             self.getpage("/test_handler")         self.assertstatus('200 ok')         self.assertheader('content-type', 'text/html;charset=utf-8')         self.assertbody('4') 

now may safely run test follows:

 $ py.test -sv cp_test.py ============================================================================================================ test session starts ============================================================================================================= platform darwin -- python 3.5.2, pytest-2.9.2, py-1.4.31, pluggy-0.3.1 -- ~/.pyenv/versions/3.5.2/envs/cptest-pyenv-virtualenv/bin/python3.5 cachedir: .cache rootdir: ~/src/cptest, inifile: collected 2 items  cp_test.py::simplecptest::test_check_two_plus_two_equals_four passed cp_test.py::simplecptest::test_gc <- ../../.pyenv/versions/3.5.2/envs/cptest-pyenv-virtualenv/lib/python3.5/site-packages/cherrypy/test/helper.py passed 

Comments