/usr/share/doc/python-webtest/docs/index.html is in python-webtest 1.3.4-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing Applications with WebTest — WebTest 1.3.4 documentation</title>
<link rel="stylesheet" href="_static/default.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '',
VERSION: '1.3.4',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true
};
</script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<link rel="top" title="WebTest 1.3.4 documentation" href="#" />
<link rel="next" title="News" href="news.html" />
</head>
<body>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
accesskey="I">index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="news.html" title="News"
accesskey="N">next</a> |</li>
<li><a href="#">WebTest 1.3.4 documentation</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body">
<div class="section" id="testing-applications-with-webtest">
<h1><a class="toc-backref" href="#id3">Testing Applications with WebTest</a><a class="headerlink" href="#testing-applications-with-webtest" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">author:</th><td class="field-body">Ian Bicking <<a class="reference external" href="mailto:ianb%40colorstudy.com">ianb<span>@</span>colorstudy<span>.</span>com</a>></td>
</tr>
<tr class="field-even field"><th class="field-name">maintainer:</th><td class="field-body">Gael Pasgrimaud <<a class="reference external" href="mailto:gael%40gawel.org">gael<span>@</span>gawel<span>.</span>org</a>></td>
</tr>
</tbody>
</table>
<div class="toctree-wrapper compound">
<ul>
<li class="toctree-l1"><a class="reference internal" href="news.html">News</a></li>
<li class="toctree-l1"><a class="reference internal" href="license.html">License</a></li>
<li class="toctree-l1"><a class="reference internal" href="modules/webtest.html"><tt class="docutils literal"><span class="pre">webtest</span></tt> – Functional Testing of Web Applications</a></li>
<li class="toctree-l1"><a class="reference internal" href="modules/sel.html"><tt class="docutils literal"><span class="pre">webtest.sel</span></tt> – Functional Testing with Selenium</a></li>
</ul>
</div>
<div class="contents topic" id="contents">
<p class="topic-title first">Contents</p>
<ul class="simple">
<li><a class="reference internal" href="#testing-applications-with-webtest" id="id3">Testing Applications with WebTest</a><ul>
<li><a class="reference internal" href="#status-license" id="id4">Status & License</a></li>
<li><a class="reference internal" href="#installation" id="id5">Installation</a></li>
<li><a class="reference internal" href="#what-this-does" id="id6">What This Does</a></li>
<li><a class="reference internal" href="#testapp" id="id7">TestApp</a><ul>
<li><a class="reference internal" href="#making-requests" id="id8">Making Requests</a><ul>
<li><a class="reference internal" href="#modifying-the-environment-simulating-authentication" id="id9">Modifying the Environment & Simulating Authentication</a></li>
<li><a class="reference internal" href="#what-is-tested-by-default" id="id10">What Is Tested By Default</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-response-object" id="id11">The Response Object</a></li>
</ul>
</li>
<li><a class="reference internal" href="#form-submissions" id="id12">Form Submissions</a></li>
<li><a class="reference internal" href="#parsing-the-body" id="id13">Parsing the Body</a></li>
<li><a class="reference internal" href="#framework-hooks" id="id14">Framework Hooks</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="status-license">
<h2><a class="toc-backref" href="#id4">Status & License</a><a class="headerlink" href="#status-license" title="Permalink to this headline">¶</a></h2>
<p>WebTest is an extraction of <tt class="docutils literal"><span class="pre">paste.fixture.TestApp</span></tt>, rewriting
portions to use <a class="reference external" href="http://docs.webob.org/">WebOb</a>. It is under
active development as part of the Paste cloud of packages.</p>
<p>Feedback and discussion should take place on the <a class="reference external" href="http://pythonpaste.org/community/">Paste list</a>, and bugs should go into the
<a class="reference external" href="https://bitbucket.org/ianb/webtest/issues?status=new&status=open">Bitbucket tracker</a>.</p>
<p>This library is licensed under an <a class="reference external" href="license.html">MIT-style license</a>.</p>
<p>WebTest is in a mercurial repository at <a class="reference external" href="https://bitbucket.org/ianb/webtest">http://bitbucket.org/ianb/webtest</a>. You can check it out with:</p>
<div class="highlight-python"><pre>$ hg clone https://bitbucket.org/ianb/webtest WebTest</pre>
</div>
</div>
<div class="section" id="installation">
<h2><a class="toc-backref" href="#id5">Installation</a><a class="headerlink" href="#installation" title="Permalink to this headline">¶</a></h2>
<p>You can use pip or easy_install to get the latest stable release:</p>
<div class="highlight-python"><pre>$ pip install WebTest
$ easy_install WebTest</pre>
</div>
<p>Or if you want the development version:</p>
<div class="highlight-python"><pre>$ pip install "http://bitbucket.org/ianb/webtest/get/tip.tar.gz#egg=WebTest"</pre>
</div>
</div>
<div class="section" id="what-this-does">
<h2><a class="toc-backref" href="#id6">What This Does</a><a class="headerlink" href="#what-this-does" title="Permalink to this headline">¶</a></h2>
<p>WebTest helps you test your WSGI-based web applications. This can be
any application that has a WSGI interface, including an application
written in a framework that supports WSGI (which includes most
actively developed Python web frameworks – almost anything that even
nominally supports WSGI should be testable).</p>
<p>With this you can test your web applications without starting an HTTP
server, and without poking into the web framework shortcutting
pieces of your application that need to be tested. The tests WebTest
runs are entirely equivalent to how a WSGI HTTP server would call an
application. By testing the full stack of your application, the
WebTest testing model is sometimes called a <em>functional test</em>,
<em>integration test</em>, or <em>acceptance test</em> (though the latter two are
not particularly good descriptions). This is in contrast to a <em>unit
test</em> which tests a particular piece of functionality in your
application. While complex programming tasks are often is suited to
unit tests, template logic and simple web programming is often best
done with functional tests; and regardless of the presence of unit
tests, no testing strategy is complete without high-level tests to
ensure the entire programming system works together.</p>
<p>WebTest helps you create tests by providing a convenient interface to
run WSGI applications and verify the output.</p>
</div>
<div class="section" id="testapp">
<h2><a class="toc-backref" href="#id7">TestApp</a><a class="headerlink" href="#testapp" title="Permalink to this headline">¶</a></h2>
<p>The most important object in WebTest is <a class="reference internal" href="modules/webtest.html#webtest.TestApp" title="webtest.TestApp"><tt class="xref py py-class docutils literal"><span class="pre">TestApp</span></tt></a>, the wrapper
for WSGI applications. To use it, you simply instantiate it with your WSGI
application. (Note: if your WSGI application requires any configuration, you
must set that up manually in your tests.)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">webtest</span> <span class="kn">import</span> <span class="n">TestApp</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">webtest.debugapp</span> <span class="kn">import</span> <span class="n">debug_app</span>
<span class="gp">>>> </span><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">debug_app</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/form.html'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">status</span>
<span class="go">'200 OK'</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">form</span>
<span class="go"><Form /></span>
</pre></div>
</div>
<div class="section" id="making-requests">
<h3><a class="toc-backref" href="#id8">Making Requests</a><a class="headerlink" href="#making-requests" title="Permalink to this headline">¶</a></h3>
<p>To make a request, use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/path'</span><span class="p">,</span> <span class="p">[</span><span class="n">headers</span><span class="p">],</span> <span class="p">[</span><span class="n">extra_environ</span><span class="p">],</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>This does a request for <tt class="docutils literal"><span class="pre">/path</span></tt>, with any extra headers or WSGI
environment keys that you indicate. This returns a response object,
based on <a class="reference external" href="http://docs.webob.org/en/latest/reference.html#response">webob.Response</a>. It has some
additional methods to make it easier to test.</p>
<p>If you want to do a POST request, use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s">'/path'</span><span class="p">,</span> <span class="p">{</span><span class="s">'vars'</span><span class="p">:</span> <span class="s">'values'</span><span class="p">},</span> <span class="p">[</span><span class="n">headers</span><span class="p">],</span> <span class="p">[</span><span class="n">extra_environ</span><span class="p">],</span>
<span class="p">[</span><span class="n">upload_files</span><span class="p">],</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>Specifically the second argument is the <em>body</em> of the request. You
can pass in a dictionary (or dictionary-like object), or a string
body (dictionary objects are turned into HTML form submissions).</p>
<p>You can also pass in the keyword argument upload_files, which is a
list of <tt class="docutils literal"><span class="pre">[(fieldname,</span> <span class="pre">filename,</span> <span class="pre">fild_content)]</span></tt>. File uploads use a
different form submission data type to pass the structured data.</p>
<p>For other verbs you can use:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="n">path</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>These do PUT and DELETE requests.</p>
<div class="section" id="modifying-the-environment-simulating-authentication">
<h4><a class="toc-backref" href="#id9">Modifying the Environment & Simulating Authentication</a><a class="headerlink" href="#modifying-the-environment-simulating-authentication" title="Permalink to this headline">¶</a></h4>
<p>The best way to simulate authentication is if your application looks
in <tt class="docutils literal"><span class="pre">environ['REMOTE_USER']</span></tt> to see if someone is authenticated.
Then you can simply set that value, like:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/secret'</span><span class="p">,</span> <span class="n">extra_environ</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">REMOTE_USER</span><span class="o">=</span><span class="s">'bob'</span><span class="p">))</span>
</pre></div>
</div>
<p>If you want <em>all</em> your requests to have this key, do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">app</span> <span class="o">=</span> <span class="n">TestApp</span><span class="p">(</span><span class="n">my_app</span><span class="p">,</span> <span class="n">extra_environ</span><span class="o">=</span><span class="nb">dict</span><span class="p">(</span><span class="n">REMOTE_USER</span><span class="o">=</span><span class="s">'bob'</span><span class="p">))</span>
</pre></div>
</div>
</div>
<div class="section" id="what-is-tested-by-default">
<h4><a class="toc-backref" href="#id10">What Is Tested By Default</a><a class="headerlink" href="#what-is-tested-by-default" title="Permalink to this headline">¶</a></h4>
<p>A key concept behind WebTest is that there’s lots of things you
shouldn’t have to check everytime you do a request. It is assumed
that the response will either be a 2xx or 3xx response; if it isn’t an
exception will be raised (you can override this for a request, of
course). The WSGI application is tested for WSGI compliance with
a slightly modified version of <a class="reference external" href="http://python.org/doc/current/lib/module-wsgiref.validate.html">wsgiref.validate</a>
(modified to support arguments to <tt class="docutils literal"><span class="pre">InputWrapper.readline</span></tt>)
automatically. Also it checks that nothing is printed to the
<tt class="docutils literal"><span class="pre">environ['wsgi.errors']</span></tt> error stream, which typically indicates a
problem (one that would be non-fatal in a production situation, but if
you are testing is something you should avoid).</p>
<p>To indicate another status is expected, use the keyword argument
<tt class="docutils literal"><span class="pre">status=404</span></tt> to (for example) check that it is a 404 status, or
<tt class="docutils literal"><span class="pre">status="*"</span></tt> to allow any status.</p>
<p>If you expect errors to be printed, use <tt class="docutils literal"><span class="pre">expect_errors=True</span></tt>.</p>
</div>
</div>
<div class="section" id="the-response-object">
<h3><a class="toc-backref" href="#id11">The Response Object</a><a class="headerlink" href="#the-response-object" title="Permalink to this headline">¶</a></h3>
<p>The response object is based on <a class="reference external" href="http://docs.webob.org/en/latest/reference.html#response">webob.Response</a> with some additions
to help with testing.</p>
<p>The inherited attributes that are most interesting:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">response.status</span></tt>:</dt>
<dd>The text status of the response, e.g., <tt class="docutils literal"><span class="pre">"200</span> <span class="pre">OK"</span></tt>.</dd>
<dt><tt class="docutils literal"><span class="pre">response.headers</span></tt>:</dt>
<dd>A dictionary-like object of the headers in the response.</dd>
<dt><tt class="docutils literal"><span class="pre">response.body</span></tt>:</dt>
<dd>The text body of the response.</dd>
<dt><tt class="docutils literal"><span class="pre">response.unicode_body</span></tt>:</dt>
<dd>The unicode text body of the response.</dd>
<dt><tt class="docutils literal"><span class="pre">response.request</span></tt>:</dt>
<dd>The <a class="reference external" href="http://docs.webob.org/en/latest/reference.html#request">webob.Request object</a> used to generate
this response.</dd>
</dl>
<p>The added methods:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">response.follow(**kw)</span></tt>:</dt>
<dd>Follows the redirect, returning the new response. It is an error
if this response wasn’t a redirect. Any keyword arguments are
passed to <tt class="docutils literal"><span class="pre">app.get</span></tt> (e.g., <tt class="docutils literal"><span class="pre">status</span></tt>).</dd>
<dt><tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">response</span></tt>:</dt>
<dd>Returns True if the string is found in the response body.
Whitespace is normalized for this test.</dd>
<dt><tt class="docutils literal"><span class="pre">response.mustcontain(string1,</span> <span class="pre">string2,</span> <span class="pre">...)</span></tt>:</dt>
<dd>Raises an error if any of the strings are not found in the
response. It also prints out the response in that case, so
you can see the real response.</dd>
<dt><tt class="docutils literal"><span class="pre">response.showbrowser()</span></tt>:</dt>
<dd>Opens the HTML response in a browser; useful for debugging.</dd>
<dt><tt class="docutils literal"><span class="pre">str(response)</span></tt>:</dt>
<dd>Gives a slightly-compacted version of the response. This is
compacted to remove newlines, making it easier to use with
<a class="reference external" href="http://python.org/doc/current/lib/module-doctest.html">doctest</a></dd>
<dt><tt class="docutils literal"><span class="pre">response.click(description=None,</span> <span class="pre">linkid=None,</span> <span class="pre">href=None,</span> <span class="pre">anchor=None,</span> <span class="pre">index=None,</span> <span class="pre">verbose=False)</span></tt>:</dt>
<dd>Clicks the described link (<a class="reference external" href="./class-webtest.TestResponse.html#click">see docstring for more</a>)</dd>
<dt><tt class="docutils literal"><span class="pre">response.forms</span></tt>:</dt>
<dd>Return a dictionary of forms; you can use both indexes (refer to
the forms in order) or the string ids of forms (if you’ve given
them ids) to identify the form. See <a class="reference internal" href="#form-submissions"><em>Form Submissions</em></a> for
more on the form objects.</dd>
<dt><tt class="docutils literal"><span class="pre">response.form</span></tt>:</dt>
<dd>If there is just a single form, this returns that. It is an error
if you use this and there are multiple forms.</dd>
</dl>
</div>
</div>
<div class="section" id="form-submissions">
<span id="id2"></span><h2><a class="toc-backref" href="#id12">Form Submissions</a><a class="headerlink" href="#form-submissions" title="Permalink to this headline">¶</a></h2>
<p>You can fill out and submit forms from your tests. First you get the
form:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">'/form.html'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">form</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">form</span>
</pre></div>
</div>
<p>Then you fill it in fields:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">action</span><span class="p">)</span>
<span class="go">/form-submit</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">form</span><span class="o">.</span><span class="n">method</span><span class="p">)</span>
<span class="go">POST</span>
<span class="gp">>>> </span><span class="c"># dict of fields</span>
<span class="gp">>>> </span><span class="n">form</span><span class="o">.</span><span class="n">fields</span><span class="o">.</span><span class="n">values</span><span class="p">()</span>
<span class="go">[(u'name', [<Text name="name">]), (u'submit', [<Submit name="submit">])]</span>
<span class="gp">>>> </span><span class="n">form</span><span class="p">[</span><span class="s">'name'</span><span class="p">]</span> <span class="o">=</span> <span class="s">'Bob'</span>
<span class="gp">>>> </span><span class="c"># When names don't point to a single field:</span>
<span class="gp">>>> </span><span class="n">form</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s">'name'</span><span class="p">,</span> <span class="s">'Bob'</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
<p>Then you can submit:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="c"># Submit with no particular submit button pressed:</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">submit</span><span class="p">()</span>
<span class="gp">>>> </span><span class="c"># Or submit a button:</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">form</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="s">'submit'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">print</span><span class="p">(</span><span class="n">res</span><span class="p">)</span>
<span class="go">Response: 200 OK</span>
<span class="go">Content-Type: text/plain</span>
<span class="gp">...</span>
<span class="go">-- Body ----------</span>
<span class="go">submit=Submit%21&name=Bob</span>
</pre></div>
</div>
<p>Select fields can only be set to valid values (i.e., values in an
<tt class="docutils literal"><span class="pre"><option></span></tt>) but you can also use
<tt class="docutils literal"><span class="pre">form['select-field'].force_value('value')</span></tt> to enter values not
present in an option.</p>
</div>
<div class="section" id="parsing-the-body">
<h2><a class="toc-backref" href="#id13">Parsing the Body</a><a class="headerlink" href="#parsing-the-body" title="Permalink to this headline">¶</a></h2>
<p>There are several ways to get parsed versions of the response. These
are the attributes:</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">response.html</span></tt>:</dt>
<dd>Return a <a class="reference external" href="http://www.crummy.com/software/BeautifulSoup/">BeautifulSoup</a> version of the
response body.</dd>
<dt><tt class="docutils literal"><span class="pre">response.xml</span></tt>:</dt>
<dd>Return an <a class="reference external" href="http://python.org/doc/current/lib/module-xml.etree.ElementTree.html">ElementTree</a>
version of the response body.</dd>
<dt><tt class="docutils literal"><span class="pre">response.lxml</span></tt>:</dt>
<dd>Return an <a class="reference external" href="http://codespeak.net/lxml/">lxml</a> version of the
response body.</dd>
<dt><tt class="docutils literal"><span class="pre">response.pyquery</span></tt>:</dt>
<dd>Return an <a class="reference external" href="http://pypi.python.org/pypi/pyquery">PyQuery</a> version of the
response body.</dd>
<dt><tt class="docutils literal"><span class="pre">response.json</span></tt>:</dt>
<dd>Return the parsed JSON (parsed with <a class="reference external" href="http://svn.red-bean.com/bob/simplejson/tags/simplejson-1.7/docs/index.html">simplejson</a>).</dd>
</dl>
<p>In each case the content-type must be correct or an AttributeError is
raised. If you do not have the necessary library installed (none of
them are required by WebTest), you will get an ImportError.</p>
<p>Examples:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">webtest</span> <span class="kn">import</span> <span class="n">TestRequest</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">webtest</span> <span class="kn">import</span> <span class="n">TestResponse</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">TestResponse</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="s">'text/html'</span><span class="p">,</span> <span class="n">body</span><span class="o">=</span><span class="n">b</span><span class="s">'''</span>
<span class="gp">... </span><span class="s"><html><body><div id="content">hey!</div></body>'''</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">request</span> <span class="o">=</span> <span class="n">TestRequest</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">html</span>
<span class="go"><html><body><div id="content">hey!</div></body></html></span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">html</span><span class="o">.</span><span class="n">__class__</span>
<span class="go"><class 'BeautifulSoup.BeautifulSoup'></span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">lxml</span>
<span class="go"><Element html at ...></span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">lxml</span><span class="o">.</span><span class="n">xpath</span><span class="p">(</span><span class="s">'//body/div'</span><span class="p">)[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
<span class="go">'hey!'</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">TestResponse</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="s">'application/json'</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">body</span><span class="o">=</span><span class="n">b</span><span class="s">'{"a":1,"b":2}'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">request</span> <span class="o">=</span> <span class="n">TestRequest</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="nb">list</span><span class="p">(</span><span class="n">res</span><span class="o">.</span><span class="n">json</span><span class="o">.</span><span class="n">values</span><span class="p">())</span>
<span class="go">[1, 2]</span>
<span class="gp">>>> </span><span class="n">res</span> <span class="o">=</span> <span class="n">TestResponse</span><span class="p">(</span><span class="n">content_type</span><span class="o">=</span><span class="s">'application/xml'</span><span class="p">,</span>
<span class="gp">... </span> <span class="n">body</span><span class="o">=</span><span class="n">b</span><span class="s">'<xml><message>hey!</message></xml>'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">request</span> <span class="o">=</span> <span class="n">TestRequest</span><span class="o">.</span><span class="n">blank</span><span class="p">(</span><span class="s">'/'</span><span class="p">)</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">xml</span>
<span class="go"><Element ...></span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">xml</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">tag</span>
<span class="go">'message'</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">xml</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
<span class="go">'hey!'</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">lxml</span>
<span class="go"><Element xml at ...></span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">lxml</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">tag</span>
<span class="go">'message'</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">lxml</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">text</span>
<span class="go">'hey!'</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">pyquery</span><span class="p">(</span><span class="s">'message'</span><span class="p">)</span>
<span class="go">[<message>]</span>
<span class="gp">>>> </span><span class="n">res</span><span class="o">.</span><span class="n">pyquery</span><span class="p">(</span><span class="s">'message'</span><span class="p">)</span><span class="o">.</span><span class="n">text</span><span class="p">()</span>
<span class="go">'hey!'</span>
</pre></div>
</div>
</div>
<div class="section" id="framework-hooks">
<h2><a class="toc-backref" href="#id14">Framework Hooks</a><a class="headerlink" href="#framework-hooks" title="Permalink to this headline">¶</a></h2>
<p>Frameworks can detect that they are in a testing environment by the
presence (and truth) of the WSGI environmental variable
<tt class="docutils literal"><span class="pre">"paste.testing"</span></tt> (the key name is inherited from
<tt class="docutils literal"><span class="pre">paste.fixture</span></tt>).</p>
<p>More generally, frameworks can detect that something (possibly a test
fixture) is ready to catch unexpected errors by the presence and truth
of <tt class="docutils literal"><span class="pre">"paste.throw_errors"</span></tt> (this is sometimes set outside of testing
fixtures too, when an error-handling middleware is in place).</p>
<p>Frameworks that want to expose the inner structure of the request may
use <tt class="docutils literal"><span class="pre">"paste.testing_variables"</span></tt>. This will be a dictionary – any
values put into that dictionary will become attributes of the response
object. So if you do <tt class="docutils literal"><span class="pre">env["paste.testing_variables"]['template']</span> <span class="pre">=</span>
<span class="pre">template_name</span></tt> in your framework, then <tt class="docutils literal"><span class="pre">response.template</span></tt> will be
<tt class="docutils literal"><span class="pre">template_name</span></tt>.</p>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<h3><a href="#">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Testing Applications with WebTest</a><ul>
<li><a class="reference internal" href="#status-license">Status & License</a></li>
<li><a class="reference internal" href="#installation">Installation</a></li>
<li><a class="reference internal" href="#what-this-does">What This Does</a></li>
<li><a class="reference internal" href="#testapp">TestApp</a><ul>
<li><a class="reference internal" href="#making-requests">Making Requests</a><ul>
<li><a class="reference internal" href="#modifying-the-environment-simulating-authentication">Modifying the Environment & Simulating Authentication</a></li>
<li><a class="reference internal" href="#what-is-tested-by-default">What Is Tested By Default</a></li>
</ul>
</li>
<li><a class="reference internal" href="#the-response-object">The Response Object</a></li>
</ul>
</li>
<li><a class="reference internal" href="#form-submissions">Form Submissions</a></li>
<li><a class="reference internal" href="#parsing-the-body">Parsing the Body</a></li>
<li><a class="reference internal" href="#framework-hooks">Framework Hooks</a></li>
</ul>
</li>
</ul>
<h4>Next topic</h4>
<p class="topless"><a href="news.html"
title="next chapter">News</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="_sources/index.txt"
rel="nofollow">Show Source</a></li>
</ul>
<div id="searchbox" style="display: none">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
<p class="searchtip" style="font-size: 90%">
Enter search terms or a module, class or function name.
</p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="genindex.html" title="General Index"
>index</a></li>
<li class="right" >
<a href="py-modindex.html" title="Python Module Index"
>modules</a> |</li>
<li class="right" >
<a href="news.html" title="News"
>next</a> |</li>
<li><a href="#">WebTest 1.3.4 documentation</a> »</li>
</ul>
</div>
<div class="footer">
© Copyright 2008, Ian Bicking.
Last updated on May 06, 2012.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
</div>
</body>
</html>
|