Source

pycon_2009_func_testing_panel / webtest / my_webtest.txt

.. include:: <s5defs.txt>

=======
WebTest
=======

:author: Ian Bicking
:organization: The Open Planning Project <http://theopenplanningproject.org>

====
What
====

* Runs fake web requests
* Sends them to any WSGI-compatible framework (*all* frameworks that matter)
* Gives you a response object to inspect

===
Why
===

* I needed to test Paste
* WSGI makes the testing interface accurate and general
* Using WebTest you can run functional tests, then inspect application internals
* Asserting things like "page response with 200 OK" is tedious, and I
  made a framework that avoided this particular tedium

====
Demo
====

::

    >>> from myapp import wsgi_application
    >>> from webtest import TestApp
    >>> app = TestApp(wsgi_application)
    >>> resp = app.get('/')
    >>> print resp
    200 OK
    Content-Type: text/html
    ...
    The body of the response

==============
Other Features
==============

* Fills out and submits forms
* Frameworks can send data through WebTest (e.g., variables used to fill a template)
* Preserves cookies
* Follows links and redirects
* Methods to intelligently check the body for text (avoiding too much fragility)
* Parses to BeautifulSoup, ElementTree, lxml, lxml.html, JSON