Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #930 (closed enhancement: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] Add standard way to unit test stateful/multiuser scenarios

Reported by: thesamet Owned by: anonymous
Priority: normal Milestone: 1.0b2
Component: TurboGears Version: 0.9a6
Severity: normal Keywords: testing identity BroserSession
Cc:

Description

The current testing tools provided by TurboGears do not give “one obvious way” to test multi-user scenarios. I introduced last week a simple class that will emulate a browser session of a single user against a cherrypy server. Its only task is actually to keep track of the visit cookie.

def cookie_header(morsel):
    """Returns a dict containing cookie information to pass to a server."""
    return {'Cookie': morsel.output(header="")[1:]}

class BrowserSession(object):
    def __init__(self):
        self.cookie_name = turbogears.config.get('visit.cookie.name', 'tg-visit')
        self.visit = None
        self.response, self.status = None, None

    def goto(self, *args, **kwargs):
        if self.visit:
            headers = kwargs.get('headers', {})
            headers.update(cookie_header(self.visit))
            kwargs['headers'] = headers
        testutil.createRequest(*args, **kwargs)
        if self.cookie_name in cherrypy.response.simple_cookie:
            self.visit = cherrypy.response.simple_cookie[self.cookie_name]
        self.response = cherrypy.response.body[0]
        self.status = cherrypy.response.status

Some usage examples appear in my blog post:  Testing Multi User TurboGears Applications

Attachments

browsing_session.patch Download (2.5 KB) - added by thesamet 13 years ago.
latest version BrowsingSession? + unit test

Change History

comment:1 Changed 13 years ago by fredlin

  • Type changed from defect to enhancement

It looks like a good stuff to be concerned

comment:2 Changed 13 years ago by jorge.vargas

  • Summary changed from Add standard way to unit test stateful/multiuser scenarios to [PATCH] Add standard way to unit test stateful/multiuser scenarios
  • Milestone changed from 1.0b1 to 1.0b2

Changed 13 years ago by thesamet

latest version BrowsingSession? + unit test

comment:3 Changed 12 years ago by alberto

  • Status changed from new to closed
  • Resolution set to fixed

Commited at [2245].

Some comments. Please generate your patches by "svn diff" form your SVN checkout. Else they're a pain to apply as they require hand editing.

Thanks :) Alberto

comment:4 Changed 12 years ago by alberto

BTW, A short example of how to use this at  http://docs.turbogears.org/1.0/RoughDocs would be awesome :)

Alberto

comment:5 Changed 12 years ago by jeffk

The addition of a few BrowsingSession?() tests to test_controllers.py in identity-enabled quickstart templates would be extremely helpful.

Note: See TracTickets for help on using tickets.