<div class="gmail_quote">I've been trying to do a better job of testing my (turbogears) web apps lately, and am trying to decide if I should be using twill or paste.fixture. I like a lot of things about twill, but one of the key strengths of paste (IMHO) is that it's "in process" testing feature includes the ability to dump the wsgi environment (unlike wsgi_intercept, which otherwise is a very nice hack). Access to the environ is very handy [1] when you're doing MVC work & trying to seperate what the controller's doing from what the rendered result looks like.
<div> </div>
<div>Anyway, code speaks louder than words, so I've included an example of what I'd like to see, and a patch to twill to make it work. I'd be interested to know if you all think I'm on the right track or if I'm all wet. I'm a twill & wsgi n00b, so my apologies if I've missed anything obvious.
<br> </div>
<div>Thanks!</div>
<div>-Ken</div>
<div> </div>
<div>[1] This is the best link I could find -- sorry, I've read so many of Titus' and Ian Bicking's posts lately that my head is spinning.. :</div>
<div><a href="http://groups.google.com/group/turbogears-trunk/browse_thread/thread/9ba8aec93a82c81b" target="_blank">http://groups.google.com/group/turbogears-trunk/browse_thread/thread/9ba8aec93a82c81b</a></div>
<div> </div>
<div> </div>
<div> </div>
<div>Example wsgi app:</div>
<div> </div>
<div>#Based off of Grig Gheorghiu's April 2006 blog entry on something Titus showed him:<br>#http://agiletesting.blogspot.com/2006/04/in-process-web-app-testing-with-twill.html</div>
<div>import twill<br>from StringIO import StringIO</div>
<div>def simple_app(environ, start_response):<br> status = '200 OK'<br> response_headers = [('Content-type','text/plain')]<br> environ['simple_app'] = "WSGI's taking over!!"
<br> start_response(status, response_headers)<br> return ['Hello world!\n']<br><br></div>
<div>twill.add_wsgi_intercept('localhost', 8001, lambda: simple_app)</div>
<div>twill.set_output(StringIO())</div>
<div>print twill.commands.go("<a href="http://localhost:8001/" target="_blank">http://localhost:8001/</a>")<br>print twill.commands.show()<br>print twill.commands.showwsgienv('simple_app')<br></div>
<div> </div>
<div>Produces the result: </div>
<div> </div>
<div><a href="http://localhost:8001/" target="_blank">http://localhost:8001/</a><br>Hello world!</div>
<div>WSGI's taking over!!<br></div>
<div> </div>
<div> </div>
<div>Hacks to twill's code to make this work: </div>
<div> </div>
<div>diff -ru twill-latest/twill/commands.py twill-hacked/twill/commands.py<br>--- twill-latest/twill/commands.py 2008-01-02 03:05:47.000000000 -0600<br>+++ twill-hacked/twill/commands.py 2008-01-02 21:01:34.000000000
-0600<br>@@ -32,6 +32,7 @@<br> 'showforms',<br> 'showlinks',<br> 'showhistory',<br>+ 'showwsgienv',<br> 'submit',<br> 'formvalue',
<br> 'fv',<br>@@ -277,6 +278,16 @@<br> print>>OUT, html<br> return html</div>
<p>+def showwsgienv(key):<br>+ """<br>+ >> showwsgienv<br>+<br>+ Show the WSGI Environment.<br>+ """<br>+ from twill.wsgi_intercept import environ<br>+ if key:
<br>+ environ = environ[key]<br>+ print>>OUT, environ
<br>+ return environ<br>+<br> def echo(*strs):<br> """<br> >> echo <list> <of> <strings><br>diff -ru twill-latest/twill/wsgi_intercept.py twill-hacked/twill/wsgi_intercept.py
<br>--- twill-latest/twill/wsgi_intercept.py 2008-01-02 03:05:47.000000000 -0600<br>+++ twill-hacked/twill/wsgi_intercept.py 2008-01-02 20:26:06.000000000 -0600<br>@@ -13,6 +13,9 @@<br> import traceback</p>
<p> debuglevel = 0<br>+<br>+environ = dict()<br>+<br> # 1 basic<br> # 2 verbose</p>
<p>@@ -68,6 +71,7 @@<br> # parse the input up to the first blank line (or its end).<br> #</p>
<p>+ global environ<br> environ = {}</p>
<p> method_line = inp.readline()<br></p>
</div><br>