<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Straight Ornamental: Functional testing in Pylons, Twill and wsgi_intercept</title>
    <link>http://grahamstratton.org/blog/public/articles/2007/05/13/functional-testing-in-pylons-twill-and-wsgi_intercept</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Ramblings from a rambler</description>
    <item>
      <title>Functional testing in Pylons, Twill and wsgi_intercept</title>
      <description>&lt;p&gt;When trying to do some functional testing in Pylons, I initially tried Paste&amp;#8217;s solution, which worked a bit, but I got stuck on trying to select multiple options in a select box. So I decided to move over to Twill, which I am already familiar with anyway.&lt;/p&gt;


	&lt;p&gt;In order to use Twill for in-process testing, Titus Brown, the developer of Twill, has provided a cunning module called wsgi_intercept. Yes, there is a clue in the name. wsgi_intercept replaces httplib.HTTPConnection, redirecting specified requests to the wsgi application. Titus has an article on &lt;a href="http://www.advogato.org/article/874.html"&gt;testing with Twill and wsgi_intercept&lt;/a&gt;.&lt;/p&gt;


	&lt;p&gt;Using the module with Pylons is quite easy.  I created a new project called squirrel, with a controller called nuts. In squirrel/tests/&lt;i&gt;init&lt;/i&gt;.py, I added:&lt;/p&gt;


&lt;pre&gt;
import twill

class TwillTestController(TestCase):

    wsgi_app = loadapp('config:test.ini', relative_to=conf_dir)

    def setUp(self):
        def build_app():
            return self.wsgi_app

        twill.add_wsgi_intercept('localhost', 8080, build_app)

    def tearDown(self):
        twill.remove_wsgi_intercept('localhost', 8080)
&lt;/pre&gt;

	&lt;p&gt;Then I changed squirrel/tests/functional/test_nuts.py to&lt;/p&gt;


&lt;pre&gt;from squirrel.tests import TwillTestController
from twill.commands import *

class TestNutsController(TwillTestController):
    def test_index(self):
        go('http://localhost:8080/')
        find('World')
        notfind('Universe')&lt;/pre&gt;

	&lt;p&gt;And running nosetests works quite nicely for me. &lt;em&gt;Unless&lt;/em&gt;, that is, http_proxy is set, which it is on my work machine. I haven&amp;#8217;t yet traced this problem to see whether it can be fixed or not, though I suspect there won&amp;#8217;t be a simple solution.&lt;/p&gt;</description>
      <pubDate>Sun, 13 May 2007 20:08:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8fea0a52-bfd8-4bee-9019-6a5d30cdb9dd</guid>
      <author>Graham Stratton</author>
      <link>http://grahamstratton.org/blog/public/articles/2007/05/13/functional-testing-in-pylons-twill-and-wsgi_intercept</link>
      <category>Pylons</category>
    </item>
  </channel>
</rss>
