<div class="gmail_quote">I&#39;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.&nbsp; I like a lot of things about twill, but one of the key strengths of paste (IMHO) is that it&#39;s &quot;in process&quot; testing feature includes the ability to dump the wsgi environment (unlike wsgi_intercept, which otherwise is a very nice hack).&nbsp;&nbsp; Access to the environ is very handy [1] when you&#39;re doing MVC work &amp; trying to seperate what the controller&#39;s doing from what the rendered result looks like.

<div>&nbsp;</div>

<div>Anyway, code speaks louder than words, so I&#39;ve included an example of what I&#39;d like to see, and a&nbsp;patch to twill to make it work.&nbsp;&nbsp; I&#39;d be interested to know if you all think I&#39;m on the right track or if I&#39;m all wet.&nbsp; I&#39;m a twill &amp; wsgi n00b, so my apologies if I&#39;ve missed anything obvious.
<br>&nbsp;</div>
<div>Thanks!</div>
<div>-Ken</div>
<div>&nbsp;</div>
<div>[1] This is the best link I could find -- sorry, I&#39;ve read so many of Titus&#39; and Ian Bicking&#39;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>&nbsp;</div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>Example wsgi app:</div>
<div>&nbsp;</div>
<div>#Based off of Grig Gheorghiu&#39;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>&nbsp;&nbsp;&nbsp; status = &#39;200 OK&#39;<br>&nbsp;&nbsp;&nbsp; response_headers = [(&#39;Content-type&#39;,&#39;text/plain&#39;)]<br>&nbsp;&nbsp;&nbsp; environ[&#39;simple_app&#39;] = &quot;WSGI&#39;s taking over!!&quot;
<br>&nbsp;&nbsp;&nbsp; start_response(status, response_headers)<br>&nbsp;&nbsp;&nbsp; return [&#39;Hello world!\n&#39;]<br><br></div>
<div>twill.add_wsgi_intercept(&#39;localhost&#39;, 8001, lambda: simple_app)</div>
<div>twill.set_output(StringIO())</div>
<div>print twill.commands.go(&quot;<a href="http://localhost:8001/" target="_blank">http://localhost:8001/</a>&quot;)<br>print twill.commands.show()<br>print twill.commands.showwsgienv(&#39;simple_app&#39;)<br></div>
<div>&nbsp;</div>
<div>Produces the result: </div>
<div>&nbsp;</div>
<div><a href="http://localhost:8001/" target="_blank">http://localhost:8001/</a><br>Hello world!</div>
<div>WSGI&#39;s taking over!!<br></div>
<div>&nbsp;</div>
<div>&nbsp;</div>
<div>Hacks to twill&#39;s code to make this work: </div>
<div>&nbsp;</div>
<div>diff -ru twill-latest/twill/commands.py twill-hacked/twill/commands.py<br>--- twill-latest/twill/commands.py&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2008-01-02 03:05:47.000000000 -0600<br>+++ twill-hacked/twill/commands.py&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2008-01-02 21:01:34.000000000

 -0600<br>@@ -32,6 +32,7 @@<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;showforms&#39;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;showlinks&#39;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;showhistory&#39;,<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;showwsgienv&#39;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;submit&#39;,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;formvalue&#39;,
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#39;fv&#39;,<br>@@ -277,6 +278,16 @@<br>&nbsp;&nbsp;&nbsp;&nbsp; print&gt;&gt;OUT, html<br>&nbsp;&nbsp;&nbsp;&nbsp; return html</div>
<p>+def showwsgienv(key):<br>+&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>+&nbsp;&nbsp;&nbsp; &gt;&gt; showwsgienv<br>+<br>+&nbsp;&nbsp;&nbsp; Show the WSGI Environment.<br>+&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>+&nbsp;&nbsp;&nbsp; from twill.wsgi_intercept import environ<br>+&nbsp;&nbsp;&nbsp; if key: &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
<br>+&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; environ = environ[key]<br>+&nbsp;&nbsp;&nbsp; print&gt;&gt;OUT, environ
<br>+&nbsp;&nbsp;&nbsp; return environ<br>+<br>&nbsp;def echo(*strs):<br>&nbsp;&nbsp;&nbsp;&nbsp; &quot;&quot;&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp; &gt;&gt; echo &lt;list&gt; &lt;of&gt; &lt;strings&gt;<br>diff -ru twill-latest/twill/wsgi_intercept.py twill-hacked/twill/wsgi_intercept.py
<br>--- twill-latest/twill/wsgi_intercept.py&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2008-01-02 03:05:47.000000000 -0600<br>+++ twill-hacked/twill/wsgi_intercept.py&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2008-01-02 20:26:06.000000000 -0600<br>@@ -13,6 +13,9 @@<br>&nbsp;import traceback</p>

<p>&nbsp;debuglevel = 0<br>+<br>+environ = dict()<br>+<br>&nbsp;# 1 basic<br>&nbsp;# 2 verbose</p>
<p>@@ -68,6 +71,7 @@<br>&nbsp;&nbsp;&nbsp;&nbsp; # parse the input up to the first blank line (or its end).<br>&nbsp;&nbsp;&nbsp;&nbsp; #</p>
<p>+&nbsp;&nbsp;&nbsp; global environ<br>&nbsp;&nbsp;&nbsp;&nbsp; environ = {}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; method_line = inp.readline()<br></p>
</div><br>