Current File : //usr/share/doc/pytest-2.7.0/html/en/xunit_setup.html |
<!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/flasky.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.7.0',
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="shortcut icon" href="_static/pytest1favi.ico"/>
<link rel="top" title="None" href="index.html" />
<link rel="up" title="pytest 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" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</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.7.0</a> »</li>
<li><a href="apiref.html" accesskey="U">pytest reference documentation</a> »</li>
</ul>
</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. You can
mix both fixture mechanisms in the same file but unittest-based
test methods cannot receive fixture arguments.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">As of pytest-2.4, teardownX functions are not called if
setupX existed and failed/was skipped. This harmonizes
behaviour across all major python testing tools.</p>
</div>
<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="sphinxsidebar">
<div class="sphinxsidebarwrapper">
<p class="logo"><a href="contents.html">
<img class="logo" src="_static/pytest1.png" alt="Logo"/>
</a></p><h3><a href="contents.html">Table Of Contents</a></h3>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="contents.html">Contents</a></li>
<li><a href="getting-started.html">Install</a></li>
<li><a href="example/index.html">Examples</a></li>
<li><a href="customize.html">Customize</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="talks.html">Talks/Posts</a></li>
<li><a href="changelog.html">Changelog</a></li>
</ul>
<hr>
<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>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li><a href="apiref.html">pytest reference documentation</a><ul>
<li>Previous: <a href="parametrize.html" title="previous chapter">Parametrizing fixtures and test functions</a></li>
<li>Next: <a href="capture.html" title="next chapter">Capturing of the stdout/stderr output</a></li>
</ul></li>
</ul></li>
</ul><h3>Useful Links</h3>
<ul>
<li><a href="index.html">The pytest Website</a></li>
<li><a href="contributing.html">Contribution Guide</a></li>
<li><a href="https://pypi.python.org/pypi/pytest">pytest @ PyPI</a></li>
<li><a href="https://bitbucket.org/pytest-dev/pytest/">pytest @ Bitbucket</a></li>
<li><a href="http://pytest.org/latest/plugins_index/index.html">3rd party plugins</a></li>
<li><a href="https://bitbucket.org/pytest-dev/pytest/issues?status=new&status=open">Issue Tracker</a></li>
<li><a href="http://pytest.org/latest/pytest.pdf">PDF Documentation</a>
</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="footer">
© Copyright 2014, holger krekel.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</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>
</body>
</html>