<!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>classic xunit-style setup</title> <link rel="stylesheet" href="_static/sphinxdoc.css" type="text/css" /> <link rel="stylesheet" href="_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '', VERSION: '2.3.4.1', 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="None" href="index.html" /> <link rel="up" title="py.test reference documentation" href="apiref.html" /> <link rel="next" title="Capturing of the stdout/stderr output" href="capture.html" /> <link rel="prev" title="Parametrizing fixtures and test functions" href="parametrize.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="capture.html" title="Capturing of the stdout/stderr output" accesskey="N">next</a></li> <li class="right" > <a href="parametrize.html" title="Parametrizing fixtures and test functions" accesskey="P">previous</a> |</li> <li><a href="contents.html">pytest-2.3.4.1</a> »</li> <li><a href="apiref.html" accesskey="U">py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <div id="searchbox" style="display: none"> <form class="search" action="search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Search" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> </div> <script type="text/javascript">$('#searchbox').show(0);</script> <h3>quicklinks</h3> <div style="text-align: left; font-size: 100%; vertical-align: middle;"> <table> <tr> <td> <a href="index.html">home</a> </td><td> <a href="contents.html">TOC/contents</a> </td></tr><tr><td> <a href="getting-started.html">install</a> </td><td> <a href="changelog.html">changelog</a> </td></tr><tr><td> <a href="example/index.html">examples</a> </td><td> <a href="customize.html">customize</a> </td></tr><tr><td> <a href="https://bitbucket.org/hpk42/pytest/issues?status=new&status=open">issues[bb]</a> </td><td> <a href="contact.html">contact</a> </td></tr></table> </div> <h3><a href="contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">classic xunit-style setup</a><ul> <li><a class="reference internal" href="#module-level-setup-teardown">Module level setup/teardown</a></li> <li><a class="reference internal" href="#class-level-setup-teardown">Class level setup/teardown</a></li> <li><a class="reference internal" href="#method-and-function-level-setup-teardown">Method and function level setup/teardown</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="parametrize.html" title="previous chapter">Parametrizing fixtures and test functions</a></p> <h4>Next topic</h4> <p class="topless"><a href="capture.html" title="next chapter">Capturing of the stdout/stderr output</a></p> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="classic-xunit-style-setup"> <span id="xunitsetup"></span><span id="classic-xunit"></span><h1>classic xunit-style setup<a class="headerlink" href="#classic-xunit-style-setup" title="Permalink to this headline">¶</a></h1> <p>This section describes a classic and popular way how you can implement fixtures (setup and teardown test state) on a per-module/class/function basis. pytest started supporting these methods around 2005 and subsequently nose and the standard library introduced them (under slightly different names). While these setup/teardown methods are and will remain fully supported you may also use pytest’s more powerful <a class="reference internal" href="fixture.html#fixture"><em>fixture mechanism</em></a> which leverages the concept of dependency injection, allowing for a more modular and more scalable approach for managing test state, especially for larger projects and for functional testing. It is safe to mix both fixture mechanisms.</p> <div class="section" id="module-level-setup-teardown"> <h2>Module level setup/teardown<a class="headerlink" href="#module-level-setup-teardown" title="Permalink to this headline">¶</a></h2> <p>If you have multiple test functions and test classes in a single module you can optionally implement the following fixture methods which will usually be called once for all the functions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">setup_module</span><span class="p">(</span><span class="n">module</span><span class="p">):</span> <span class="sd">""" setup any state specific to the execution of the given module."""</span> <span class="k">def</span> <span class="nf">teardown_module</span><span class="p">(</span><span class="n">module</span><span class="p">):</span> <span class="sd">""" teardown any state that was previously setup with a setup_module</span> <span class="sd"> method.</span> <span class="sd"> """</span> </pre></div> </div> </div> <div class="section" id="class-level-setup-teardown"> <h2>Class level setup/teardown<a class="headerlink" href="#class-level-setup-teardown" title="Permalink to this headline">¶</a></h2> <p>Similarly, the following methods are called at class level before and after all test methods of the class are called:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@classmethod</span> <span class="k">def</span> <span class="nf">setup_class</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span> <span class="sd">""" setup any state specific to the execution of the given class (which</span> <span class="sd"> usually contains tests).</span> <span class="sd"> """</span> <span class="nd">@classmethod</span> <span class="k">def</span> <span class="nf">teardown_class</span><span class="p">(</span><span class="n">cls</span><span class="p">):</span> <span class="sd">""" teardown any state that was previously setup with a call to</span> <span class="sd"> setup_class.</span> <span class="sd"> """</span> </pre></div> </div> </div> <div class="section" id="method-and-function-level-setup-teardown"> <h2>Method and function level setup/teardown<a class="headerlink" href="#method-and-function-level-setup-teardown" title="Permalink to this headline">¶</a></h2> <p>Similarly, the following methods are called around each method invocation:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">setup_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method</span><span class="p">):</span> <span class="sd">""" setup any state tied to the execution of the given method in a</span> <span class="sd"> class. setup_method is invoked for every test method of a class.</span> <span class="sd"> """</span> <span class="k">def</span> <span class="nf">teardown_method</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">method</span><span class="p">):</span> <span class="sd">""" teardown any state that was previously setup with a setup_method</span> <span class="sd"> call.</span> <span class="sd"> """</span> </pre></div> </div> <p>If you would rather define test functions directly at module level you can also use the following functions to implement fixtures:</p> <div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">setup_function</span><span class="p">(</span><span class="n">function</span><span class="p">):</span> <span class="sd">""" setup any state tied to the execution of the given function.</span> <span class="sd"> Invoked for every test function in the module.</span> <span class="sd"> """</span> <span class="k">def</span> <span class="nf">teardown_function</span><span class="p">(</span><span class="n">function</span><span class="p">):</span> <span class="sd">""" teardown any state that was previously setup with a setup_function</span> <span class="sd"> call.</span> <span class="sd"> """</span> </pre></div> </div> <p>Note that it is possible for setup/teardown pairs to be invoked multiple times per testing process.</p> </div> </div> </div> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="capture.html" title="Capturing of the stdout/stderr output" >next</a></li> <li class="right" > <a href="parametrize.html" title="Parametrizing fixtures and test functions" >previous</a> |</li> <li><a href="contents.html">pytest-2.3.4.1</a> »</li> <li><a href="apiref.html" >py.test reference documentation</a> »</li> <g:plusone></g:plusone> </ul> </div> <div class="footer"> © Copyright 2012, holger krekel. Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-7597274-13']); _gaq.push(['_trackPageview']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> <script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script> </body> </html>