This is what I hacked so far to get my TurboGears pages tested using
wsgi interception.

#wsgi_testutil.py
# Many code snippets stolen from Titus Brown
# http://www.advogato.org/article/874.html
from turbogears import testutil
from tgjob.controllers import Root
import cherrypy
from twill import wsgi_intercept

class WSGITest(testutil.DBTest):
    def setUp(self):
        testutil.DBTest.setUp(self)

        _cached_app = {}
        ### dynamically created function to build & return a WSGI app
        ### for a CherryPy Web app.
        def get_wsgi_app(_cached_app=_cached_app):
            if not _cached_app:
                cherrypy.root = Root()
                # configure cherrypy to be quiet ;)
                #cherrypy.config.update({ "server.logToScreen" :
False })
                testutil.start_cp()
                # get WSGI app.
                from cherrypy._cpwsgi import wsgiApp
                _cached_app['app'] = wsgiApp
            return _cached_app['app']

        wsgi_intercept.add_wsgi_intercept('localhost', 80,
get_wsgi_app)

    def tearDown(self):
        wsgi_intercept.remove_wsgi_intercept('localhost', 80)
        # shut down the cherrypy server.
        #cherrypy.server.stop()
        testutil.DBTest.tearDown(self)


And here is a example using the WSGITest class.

#test_views.py
# Some code snippets stolen from Titus Brown
# http://www.advogato.org/article/874.html
from turbogears import testutil, database
import tgjob.model
from data import setup_model_data
import cherrypy
import wsgi_testutil

database.set_db_uri("sqlite:///:memory:")

class TestPages(wsgi_testutil.WSGITest):

    model = tgjob.model

    def test_open_job_using_mechanize(self):
        "The new job offer page saves job details"
        from twill._browser import PatchedMechanizeBrowser as Browser
        b = Browser()
        b.open("http://localhost/";)
        # setup_model_data() initializes some records. For unknown
reasons it only works after b.open was called
        setup_model_data()
        # load the page with the set up data
        b.open("http://localhost/";)
        r = b.follow_link(text=r"Senior Python Developer")
        assert b.viewing_html()

    def test_save_job_using_twill(self):
        "The new job offer page saves job details"
        import twill
        # while we're at it, snarf twill's output.
        from StringIO import StringIO
        outp = StringIO()
        twill.set_output(outp)
        from twill.commands import *
        go("http://localhost/";)
        setup_model_data()
        go("http://localhost/";)
        follow(u'Place a new job offer')
        formvalue(1, "title", "fooTitle")
        formvalue(1, "company", "fooCompany")
        formvalue(1, "place", "fooPlace")
        formvalue(1, "homepage", "fooHomepage")
        formvalue(1, "description", "fooDescription")
        formvalue(1, "contact", "fooContact")
        submit()
        url("http://localhost";)
#         from twill import get_browser
#         b = get_browser()
#         b.go("http://localhost/";)
#         setup_model_data()
#         b.go("http://localhost/";)
#         b.follow_link(b.find_link(u'Place a new job offer'))
#         ...

Hope that helps someone.

Bastian

On 28 Mrz., 13:10, "Bastian" <[EMAIL PROTECTED]> wrote:
> Hi Andrew,
>
> just yesterday I solved the same problem you had with mechanize. Titus
> wrote wsgi_intercept (http://darcs.idyll.org/~t/projects/
> wsgi_intercept/README.html). He gave me some advice on how to setup
> wsgi interception with mechanize as the old version of wsgi_intercept
> doesn't work with current mechanize. I'm now using the
> wsgi_interception stuff from the current twill egg.
>
> I'm planning to write a short wiki entry how to do the setup. My code
> is in a very raw/hacked state and I might need some free afternoons to
> find a clean solution to post. I will later try playing with
> wsgi_intercepted twill to find out which one works better for me:
> twill or mechanize.
>
> See alsohttp://ivory.idyll.org/articles/twill-and-wsgi_intercept.html
> if you haven't already.
>
> Bastian
>
> On Mar 28, 9:47 am, "Andrew Dalke" <[EMAIL PROTECTED]> wrote:
>
> > On Mar 28, 8:59 am, "Andrew Dalke" <[EMAIL PROTECTED]> wrote:
>
> > > I've been working on understanding how to do testing of TG apps.
> > > The TG book mentions mechanize.
>
> > In passing it mentions twill, and I've seen Titus' blogs on that
> > package.
>
> > I looked at twill just now.  It supports calling a server through
> > wsgi,
> > which seems better than my hacked-up adapter.
>
> > Is that how people usually use twill to talk to a TG app for doing
> > unit tests?
>
> >         Andrew
> >         dalke @ dalke scientific . com


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"TurboGears" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/turbogears?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to