Current File : //usr/share/doc/pytest-2.7.0/html/en/mark.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>Marking test functions with attributes</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="Skip and xfail: dealing with tests that can not succeed" href="skipping.html" />
    <link rel="prev" title="Temporary directories and files" href="tmpdir.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="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.7.0</a> &raquo;</li>
          <li><a href="apiref.html" accesskey="U">pytest reference documentation</a> &raquo;</li> 
      </ul>
    </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 &#8220;expected failure&#8221; 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&#8217;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">pytest.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">@pytest.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 &#8216;slowtest&#8217; <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">pytest</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">pytest</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>
<dl class="docutils">
<dt>When a MarkDecorator instance is called it does the following:</dt>
<dd><ol class="first last arabic simple">
<li>If called with a single class as its only positional argument and no
additional keyword arguments, it attaches itself to the class so it
gets applied automatically to all test cases found in that class.</li>
<li>If called with a single function as its only positional argument and
no additional keyword arguments, it attaches a MarkInfo object to the
function, containing all the arguments already stored internally in
the MarkDecorator.</li>
<li>When called in any other case, it performs a &#8216;fake construction&#8217; call,
i.e. it returns a new MarkDecorator instance with the original
MarkDecorator&#8217;s content updated with the arguments passed to this
call.</li>
</ol>
</dd>
</dl>
<p>Note: The rules above prevent MarkDecorator objects from storing only a
single function or class reference as their positional argument with no
additional keyword or positional arguments.</p>
</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="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="#">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>
<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="tmpdir.html" title="previous chapter">Temporary directories and files</a></li>
      <li>Next: <a href="skipping.html" title="next chapter">Skip and xfail: dealing with tests that can not succeed</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">
    &copy; 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>