<!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>Marking test functions with attributes</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="Skip and xfail: dealing with tests that can not succeed" href="skipping.html" /> <link rel="prev" title="Temporary directories and files" href="tmpdir.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="skipping.html" title="Skip and xfail: dealing with tests that can not succeed" accesskey="N">next</a></li> <li class="right" > <a href="tmpdir.html" title="Temporary directories and files" 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="#">Marking test functions with attributes</a><ul> <li><a class="reference internal" href="#api-reference-for-mark-related-objects">API reference for mark related objects</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="tmpdir.html" title="previous chapter">Temporary directories and files</a></p> <h4>Next topic</h4> <p class="topless"><a href="skipping.html" title="next chapter">Skip and xfail: dealing with tests that can not succeed</a></p> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="marking-test-functions-with-attributes"> <span id="mark"></span><h1>Marking test functions with attributes<a class="headerlink" href="#marking-test-functions-with-attributes" title="Permalink to this headline">¶</a></h1> <p>By using the <tt class="docutils literal"><span class="pre">pytest.mark</span></tt> helper you can easily set metadata on your test functions. There are some builtin markers, for example:</p> <ul class="simple"> <li><a class="reference internal" href="skipping.html#skipif"><em>skipif</em></a> - skip a test function if a certain condition is met</li> <li><a class="reference internal" href="skipping.html#xfail"><em>xfail</em></a> - produce an “expected failure” outcome if a certain condition is met</li> <li><a class="reference internal" href="parametrize.html#parametrizemark"><em>parametrize</em></a> to perform multiple calls to the same test function.</li> </ul> <p>It’s easy to create custom markers or to apply markers to whole test classes or modules. See <a class="reference internal" href="example/markers.html#mark-examples"><em>Working with custom markers</em></a> for examples which also serve as documentation.</p> <div class="section" id="api-reference-for-mark-related-objects"> <h2>API reference for mark related objects<a class="headerlink" href="#api-reference-for-mark-related-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="_pytest.mark.MarkGenerator"> <em class="property">class </em><tt class="descname">MarkGenerator</tt><a class="reference internal" href="_modules/_pytest/mark.html#MarkGenerator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.mark.MarkGenerator" title="Permalink to this definition">¶</a></dt> <dd><p>Factory for <a class="reference internal" href="#_pytest.mark.MarkDecorator" title="_pytest.mark.MarkDecorator"><tt class="xref py py-class docutils literal"><span class="pre">MarkDecorator</span></tt></a> objects - exposed as a <tt class="docutils literal"><span class="pre">py.test.mark</span></tt> singleton instance. Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">py</span> <span class="nd">@py.test.mark.slowtest</span> <span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span> <span class="k">pass</span> </pre></div> </div> <p>will set a ‘slowtest’ <a class="reference internal" href="#_pytest.mark.MarkInfo" title="_pytest.mark.MarkInfo"><tt class="xref py py-class docutils literal"><span class="pre">MarkInfo</span></tt></a> object on the <tt class="docutils literal"><span class="pre">test_function</span></tt> object.</p> </dd></dl> <dl class="class"> <dt id="_pytest.mark.MarkDecorator"> <em class="property">class </em><tt class="descname">MarkDecorator</tt><big>(</big><em>name</em>, <em>args=None</em>, <em>kwargs=None</em><big>)</big><a class="reference internal" href="_modules/_pytest/mark.html#MarkDecorator"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.mark.MarkDecorator" title="Permalink to this definition">¶</a></dt> <dd><p>A decorator for test functions and test classes. When applied it will create <a class="reference internal" href="#_pytest.mark.MarkInfo" title="_pytest.mark.MarkInfo"><tt class="xref py py-class docutils literal"><span class="pre">MarkInfo</span></tt></a> objects which may be <a class="reference internal" href="example/simple.html#excontrolskip"><em>retrieved by hooks as item keywords</em></a>. MarkDecorator instances are often created like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="n">mark1</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">NAME</span> <span class="c"># simple MarkDecorator</span> <span class="n">mark2</span> <span class="o">=</span> <span class="n">py</span><span class="o">.</span><span class="n">test</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">NAME</span><span class="p">(</span><span class="n">name1</span><span class="o">=</span><span class="n">value</span><span class="p">)</span> <span class="c"># parametrized MarkDecorator</span> </pre></div> </div> <p>and can then be applied as decorators to test functions:</p> <div class="highlight-python"><div class="highlight"><pre><span class="nd">@mark2</span> <span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span> <span class="k">pass</span> </pre></div> </div> </dd></dl> <dl class="class"> <dt id="_pytest.mark.MarkInfo"> <em class="property">class </em><tt class="descname">MarkInfo</tt><big>(</big><em>name</em>, <em>args</em>, <em>kwargs</em><big>)</big><a class="reference internal" href="_modules/_pytest/mark.html#MarkInfo"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.mark.MarkInfo" title="Permalink to this definition">¶</a></dt> <dd><p>Marking object created by <a class="reference internal" href="#_pytest.mark.MarkDecorator" title="_pytest.mark.MarkDecorator"><tt class="xref py py-class docutils literal"><span class="pre">MarkDecorator</span></tt></a> instances.</p> <dl class="attribute"> <dt id="_pytest.mark.MarkInfo.name"> <tt class="descname">name</tt><em class="property"> = None</em><a class="headerlink" href="#_pytest.mark.MarkInfo.name" title="Permalink to this definition">¶</a></dt> <dd><p>name of attribute</p> </dd></dl> <dl class="attribute"> <dt id="_pytest.mark.MarkInfo.args"> <tt class="descname">args</tt><em class="property"> = None</em><a class="headerlink" href="#_pytest.mark.MarkInfo.args" title="Permalink to this definition">¶</a></dt> <dd><p>positional argument list, empty if none specified</p> </dd></dl> <dl class="attribute"> <dt id="_pytest.mark.MarkInfo.kwargs"> <tt class="descname">kwargs</tt><em class="property"> = None</em><a class="headerlink" href="#_pytest.mark.MarkInfo.kwargs" title="Permalink to this definition">¶</a></dt> <dd><p>keyword argument dictionary, empty if nothing specified</p> </dd></dl> <dl class="method"> <dt id="_pytest.mark.MarkInfo.add"> <tt class="descname">add</tt><big>(</big><em>args</em>, <em>kwargs</em><big>)</big><a class="reference internal" href="_modules/_pytest/mark.html#MarkInfo.add"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.mark.MarkInfo.add" title="Permalink to this definition">¶</a></dt> <dd><p>add a MarkInfo with the given args and kwargs.</p> </dd></dl> </dd></dl> </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="skipping.html" title="Skip and xfail: dealing with tests that can not succeed" >next</a></li> <li class="right" > <a href="tmpdir.html" title="Temporary directories and files" >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>