<!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="X-UA-Compatible" content="IE=Edge" /> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Testing Applications with Paste — Paste 3.0.0 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" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></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> <script type="text/javascript" src="_static/language_data.js"></script> <link rel="index" title="Index" href="genindex.html" /> <link rel="search" title="Search" href="search.html" /> <link rel="next" title="URL Parsing With WSGI And Paste" href="url-parsing-with-wsgi.html" /> <link rel="prev" title="The Future Of Paste" href="future.html" /> <link rel="stylesheet" href="_static/custom.css" type="text/css" /> <meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" /> </head><body> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body" role="main"> <div class="section" id="testing-applications-with-paste"> <h1><a class="toc-backref" href="#id2">Testing Applications with Paste</a><a class="headerlink" href="#testing-applications-with-paste" 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">revision:</th><td class="field-body">$Rev$</td> </tr> <tr class="field-odd field"><th class="field-name">date:</th><td class="field-body">$LastChangedDate$</td> </tr> </tbody> </table> <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-paste" id="id2">Testing Applications with Paste</a><ul> <li><a class="reference internal" href="#introduction" id="id3">Introduction</a></li> <li><a class="reference internal" href="#the-tests-themselves" id="id4">The Tests Themselves</a></li> <li><a class="reference internal" href="#id1" id="id5">Form Submissions</a></li> <li><a class="reference internal" href="#framework-hooks" id="id6">Framework Hooks</a></li> </ul> </li> </ul> </div> <div class="section" id="introduction"> <h2><a class="toc-backref" href="#id3">Introduction</a><a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2> <p>Paste includes functionality for testing your application in a convenient manner. These facilities are quite young, and feedback is invited. Feedback and discussion should take place on the <a class="reference external" href="http://groups.google.com/group/paste-users">Paste-users list</a>.</p> <p>These facilities let you test your Paste and WSGI-based applications easily and without a server.</p> <p>If you have questions about this document, please contact the <a class="reference external" href="http://groups.google.com/group/paste-users">paste mailing list</a> or try IRC (<code class="docutils literal notranslate"><span class="pre">#pythonpaste</span></code> on freenode.net). If there’s something that confused you and you want to give feedback, please <a class="reference external" href="http://pythonpaste.org/trac/newticket?component=documentation">submit an issue</a>.</p> </div> <div class="section" id="the-tests-themselves"> <h2><a class="toc-backref" href="#id4">The Tests Themselves</a><a class="headerlink" href="#the-tests-themselves" title="Permalink to this headline">¶</a></h2> <p>The <code class="docutils literal notranslate"><span class="pre">app</span></code> object is a wrapper around your application, with many methods to make testing convenient. Here’s an example test script:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_myapp</span><span class="p">():</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="s1">'/view'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">'id'</span><span class="p">:</span> <span class="mi">10</span><span class="p">})</span> <span class="c1"># We just got /view?id=10</span> <span class="n">res</span><span class="o">.</span><span class="n">mustcontain</span><span class="p">(</span><span class="s1">'Item 10'</span><span class="p">)</span> <span class="n">res</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">post</span><span class="p">(</span><span class="s1">'/view'</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="p">{</span><span class="s1">'id'</span><span class="p">:</span> <span class="mi">10</span><span class="p">,</span> <span class="s1">'name'</span><span class="p">:</span> <span class="s1">'New item</span> <span class="n">name</span><span class="s1">'})</span> <span class="c1"># The app does POST-and-redirect...</span> <span class="n">res</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">follow</span><span class="p">()</span> <span class="k">assert</span> <span class="n">res</span><span class="o">.</span><span class="n">request</span><span class="o">.</span><span class="n">url</span> <span class="o">==</span> <span class="s1">'/view?id=10'</span> <span class="n">res</span><span class="o">.</span><span class="n">mustcontain</span><span class="p">(</span><span class="s1">'New item name'</span><span class="p">)</span> <span class="n">res</span><span class="o">.</span><span class="n">mustcontain</span><span class="p">(</span><span class="s1">'Item updated'</span><span class="p">)</span> </pre></div> </div> <p>The methods of the <code class="docutils literal notranslate"><span class="pre">app</span></code> object (a <code class="docutils literal notranslate"><span class="pre">paste.tests.fixture.TestApp</span></code> object):</p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">get(url,</span> <span class="pre">params={},</span> <span class="pre">headers={},</span> <span class="pre">status=None)</span></code>:</dt> <dd><p class="first">Gets the URL. URLs are based in the root of your application; no domains are allowed. Parameters can be given as a dictionary, or included directly in the <code class="docutils literal notranslate"><span class="pre">url</span></code>. Headers can also be added.</p> <p>This tests that the status is a <code class="docutils literal notranslate"><span class="pre">200</span> <span class="pre">OK</span></code> or a redirect header, unless you pass in a <code class="docutils literal notranslate"><span class="pre">status</span></code>. A status of <code class="docutils literal notranslate"><span class="pre">"*"</span></code> will never fail; or you can assert a specific status (like <code class="docutils literal notranslate"><span class="pre">500</span></code>).</p> <p class="last">Also, if any errors are written to the error stream this will raise an error.</p> </dd> <dt><code class="docutils literal notranslate"><span class="pre">post(url,</span> <span class="pre">params={},</span> <span class="pre">headers={},</span> <span class="pre">status=None,</span> <span class="pre">upload_files=())</span></code>:</dt> <dd><p class="first">POSTS to the URL. Like GET, except also allows for uploading files. The uploaded files are a list of <code class="docutils literal notranslate"><span class="pre">(field_name,</span> <span class="pre">filename,</span> <span class="pre">file_content)</span></code>.</p> <p class="last">If you don’t want to do a urlencoded post body, you can put a <code class="docutils literal notranslate"><span class="pre">content-type</span></code> header in your header, and pass the body in as a string with <code class="docutils literal notranslate"><span class="pre">params</span></code>.</p> </dd> </dl> <p>The response object:</p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">header(header_name,</span> <span class="pre">[default])</span></code>:</dt> <dd>Returns the named header. It’s an error if there is more than one matching header. If you don’t provide a default, it is an error if there is no matching header.</dd> <dt><code class="docutils literal notranslate"><span class="pre">all_headers(header_name):</span></code></dt> <dd>Returns a list of all matching headers.</dd> <dt><code class="docutils literal notranslate"><span class="pre">follow(**kw)</span></code>:</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 <code class="docutils literal notranslate"><span class="pre">app.get</span></code> (e.g., <code class="docutils literal notranslate"><span class="pre">status</span></code>).</dd> <dt><code class="docutils literal notranslate"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">res</span></code>:</dt> <dd>Returns True if the string is found in the response. Whitespace is normalized for this test.</dd> <dt><code class="docutils literal notranslate"><span class="pre">mustcontain(*strings)</span></code>:</dt> <dd>Raises an error if any of the strings are not found in the response.</dd> <dt><code class="docutils literal notranslate"><span class="pre">showbrowser()</span></code>:</dt> <dd>Opens the HTML response in a browser; useful for debugging.</dd> <dt><code class="docutils literal notranslate"><span class="pre">str(res)</span></code>:</dt> <dd>Gives a slightly-compacted version of the response.</dd> <dt><code class="docutils literal notranslate"><span class="pre">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></code>:</dt> <dd>Clicks the described link (<a class="reference external" href="./class-paste.fixture.TestResponse.html#click">see docstring for more</a>)</dd> <dt><code class="docutils literal notranslate"><span class="pre">forms</span></code>:</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 external" href="#form-submissions">Form Submissions</a> for more on the form objects.</dd> </dl> <p>Request objects:</p> <dl class="docutils"> <dt><code class="docutils literal notranslate"><span class="pre">url</span></code>:</dt> <dd>The url requested.</dd> <dt><code class="docutils literal notranslate"><span class="pre">environ</span></code>:</dt> <dd>The environment used for the request.</dd> <dt><code class="docutils literal notranslate"><span class="pre">full_url</span></code>:</dt> <dd>The url with query string.</dd> </dl> </div> <div class="section" id="id1"> <h2><a class="toc-backref" href="#id5">Form Submissions</a><a class="headerlink" href="#id1" 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-default notranslate"><div class="highlight"><pre><span></span><span class="n">res</span> <span class="o">=</span> <span class="n">testapp</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">'/entry_form'</span><span class="p">)</span> <span class="n">form</span> <span class="o">=</span> <span class="n">res</span><span class="o">.</span><span class="n">forms</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> </pre></div> </div> <p>Then you fill it in fields:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># when there's one unambiguous name field:</span> <span class="n">form</span><span class="p">[</span><span class="s1">'name'</span><span class="p">]</span> <span class="o">=</span> <span class="s1">'Bob'</span> <span class="c1"># Enter something into the first field named 'age'</span> <span class="n">form</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s1">'age'</span><span class="p">,</span> <span class="s1">'45'</span><span class="p">,</span> <span class="n">index</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> </pre></div> </div> <p>Finally you submit:</p> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># Submit with no particular submit button pressed:</span> <span class="n">form</span><span class="o">.</span><span class="n">submit</span><span class="p">()</span> <span class="c1"># Or submit a button:</span> <span class="n">form</span><span class="o">.</span><span class="n">submit</span><span class="p">(</span><span class="s1">'submit_button_name'</span><span class="p">)</span> </pre></div> </div> </div> <div class="section" id="framework-hooks"> <h2><a class="toc-backref" href="#id6">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 <code class="docutils literal notranslate"><span class="pre">"paste.testing"</span></code>.</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 <code class="docutils literal notranslate"><span class="pre">"paste.throw_errors"</span></code> (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 <code class="docutils literal notranslate"><span class="pre">"paste.testing_variables"</span></code>. This will be a dictionary – any values put into that dictionary will become attributes of the response object. So if you do <code class="docutils literal notranslate"><span class="pre">env["paste.testing_variables"]['template']</span> <span class="pre">=</span> <span class="pre">template_name</span></code> in your framework, then <code class="docutils literal notranslate"><span class="pre">response.template</span></code> will be <code class="docutils literal notranslate"><span class="pre">template_name</span></code>.</p> </div> </div> </div> </div> </div> <div class="sphinxsidebar" role="navigation" aria-label="main navigation"> <div class="sphinxsidebarwrapper"> <h1 class="logo"><a href="index.html">Paste</a></h1> <h3>Navigation</h3> <ul class="current"> <li class="toctree-l1"><a class="reference internal" href="news.html">News</a></li> <li class="toctree-l1"><a class="reference internal" href="future.html">The Future Of Paste</a></li> <li class="toctree-l1 current"><a class="current reference internal" href="#">Testing Applications with Paste</a></li> <li class="toctree-l1"><a class="reference internal" href="url-parsing-with-wsgi.html">URL Parsing With WSGI And Paste</a></li> <li class="toctree-l1"><a class="reference internal" href="do-it-yourself-framework.html">A Do-It-Yourself Framework</a></li> <li class="toctree-l1"><a class="reference internal" href="paste-httpserver-threadpool.html">The Paste HTTP Server Thread Pool</a></li> <li class="toctree-l1"><a class="reference internal" href="developer-features.html">Features</a></li> <li class="toctree-l1"><a class="reference internal" href="DeveloperGuidelines.html">Python Paste Developer Guide</a></li> <li class="toctree-l1"><a class="reference internal" href="StyleGuide.html">Paste Style Guide</a></li> <li class="toctree-l1"><a class="reference internal" href="paste-httpserver-threadpool.html">The Paste HTTP Server Thread Pool</a></li> <li class="toctree-l1 current"><a class="current reference internal" href="#">Testing Applications with Paste</a></li> <li class="toctree-l1"><a class="reference internal" href="url-parsing-with-wsgi.html">URL Parsing With WSGI And Paste</a></li> <li class="toctree-l1"><a class="reference internal" href="community/index.html">Community</a></li> <li class="toctree-l1"><a class="reference internal" href="community/mailing-list.html">Mailing Lists</a></li> <li class="toctree-l1"><a class="reference internal" href="community/repository.html">Repository</a></li> <li class="toctree-l1"><a class="reference internal" href="download/index.html">Downloads</a></li> </ul> <div class="relations"> <h3>Related Topics</h3> <ul> <li><a href="index.html">Documentation overview</a><ul> <li>Previous: <a href="future.html" title="previous chapter">The Future Of Paste</a></li> <li>Next: <a href="url-parsing-with-wsgi.html" title="next chapter">URL Parsing With WSGI And Paste</a></li> </ul></li> </ul> </div> <div id="searchbox" style="display: none" role="search"> <h3>Quick search</h3> <div class="searchformwrapper"> <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> </div> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="footer"> ©2008, Ian Bicking. | Powered by <a href="http://sphinx-doc.org/">Sphinx 1.8.4</a> & <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.8</a> | <a href="_sources/testing-applications.txt" rel="nofollow">Page source</a> </div> </body> </html>