<!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>Working with custom markers</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="Usages and Examples" href="index.html" /> <link rel="next" title="A sesssion-fixture which can look at all collected tests" href="special.html" /> <link rel="prev" title="Parametrizing tests" href="parametrize.html" /> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="special.html" title="A sesssion-fixture which can look at all collected tests" accesskey="N">next</a></li> <li class="right" > <a href="parametrize.html" title="Parametrizing tests" accesskey="P">previous</a> |</li> <li><a href="../contents.html">pytest-2.3.4.1</a> »</li> <li><a href="index.html" accesskey="U">Usages and Examples</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="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="#">Working with custom markers</a><ul> <li><a class="reference internal" href="#marking-test-functions-and-selecting-them-for-a-run">Marking test functions and selecting them for a run</a></li> <li><a class="reference internal" href="#using-k-expr-to-select-tests-based-on-their-name">Using <tt class="docutils literal"><span class="pre">-k</span> <span class="pre">expr</span></tt> to select tests based on their name</a></li> <li><a class="reference internal" href="#registering-markers">Registering markers</a></li> <li><a class="reference internal" href="#marking-whole-classes-or-modules">Marking whole classes or modules</a></li> <li><a class="reference internal" href="#custom-marker-and-command-line-option-to-control-test-runs">Custom marker and command line option to control test runs</a></li> <li><a class="reference internal" href="#reading-markers-which-were-set-from-multiple-places">Reading markers which were set from multiple places</a></li> <li><a class="reference internal" href="#marking-platform-specific-tests-with-pytest">marking platform specific tests with pytest</a></li> <li><a class="reference internal" href="#automatically-adding-markers-based-on-test-names">Automatically adding markers based on test names</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="parametrize.html" title="previous chapter">Parametrizing tests</a></p> <h4>Next topic</h4> <p class="topless"><a href="special.html" title="next chapter">A sesssion-fixture which can look at all collected tests</a></p> </div> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="working-with-custom-markers"> <span id="mark-examples"></span><h1>Working with custom markers<a class="headerlink" href="#working-with-custom-markers" title="Permalink to this headline">¶</a></h1> <p>Here are some example using the <a class="reference internal" href="../mark.html#mark"><em>Marking test functions with attributes</em></a> mechanism.</p> <div class="section" id="marking-test-functions-and-selecting-them-for-a-run"> <h2>Marking test functions and selecting them for a run<a class="headerlink" href="#marking-test-functions-and-selecting-them-for-a-run" title="Permalink to this headline">¶</a></h2> <p>You can “mark” a test function with custom metadata like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_server.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="nd">@pytest.mark.webtest</span> <span class="k">def</span> <span class="nf">test_send_http</span><span class="p">():</span> <span class="k">pass</span> <span class="c"># perform some webtest test for your app</span> <span class="k">def</span> <span class="nf">test_something_quick</span><span class="p">():</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">test_another</span><span class="p">():</span> <span class="k">pass</span> </pre></div> </div> <p class="versionadded"> <span class="versionmodified">New in version 2.2.</span></p> <p>You can then restrict a test run to only run tests marked with <tt class="docutils literal"><span class="pre">webtest</span></tt>:</p> <div class="highlight-python"><pre>$ py.test -v -m webtest =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python collecting ... collected 3 items test_server.py:3: test_send_http PASSED =================== 2 tests deselected by "-m 'webtest'" =================== ================== 1 passed, 2 deselected in 0.01 seconds ==================</pre> </div> <p>Or the inverse, running all tests except the webtest ones:</p> <div class="highlight-python"><pre>$ py.test -v -m "not webtest" =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python collecting ... collected 3 items test_server.py:6: test_something_quick PASSED test_server.py:8: test_another PASSED ================= 1 tests deselected by "-m 'not webtest'" ================= ================== 2 passed, 1 deselected in 0.01 seconds ==================</pre> </div> </div> <div class="section" id="using-k-expr-to-select-tests-based-on-their-name"> <h2>Using <tt class="docutils literal"><span class="pre">-k</span> <span class="pre">expr</span></tt> to select tests based on their name<a class="headerlink" href="#using-k-expr-to-select-tests-based-on-their-name" title="Permalink to this headline">¶</a></h2> <p>You can use the <tt class="docutils literal"><span class="pre">-k</span></tt> command line option to specify an expression which implements a substring match on the test names instead of the exact match on markers that <tt class="docutils literal"><span class="pre">-m</span></tt> provides. This makes it easy to select tests based on their names:</p> <div class="highlight-python"><pre>$ py.test -v -k http # running with the above defined example module =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python collecting ... collected 3 items test_server.py:3: test_send_http PASSED ====================== 2 tests deselected by '-khttp' ====================== ================== 1 passed, 2 deselected in 0.01 seconds ==================</pre> </div> <p>And you can also run all tests except the ones that match the keyword:</p> <div class="highlight-python"><pre>$ py.test -k "not send_http" -v =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python collecting ... collected 3 items test_server.py:6: test_something_quick PASSED test_server.py:8: test_another PASSED ================= 1 tests deselected by '-knot send_http' ================== ================== 2 passed, 1 deselected in 0.01 seconds ==================</pre> </div> <p>Or to select “http” and “quick” tests:</p> <div class="highlight-python"><pre>$ py.test -k "http or quick" -v =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 -- /home/hpk/p/pytest/.tox/regen/bin/python collecting ... collected 3 items test_server.py:3: test_send_http PASSED test_server.py:6: test_something_quick PASSED ================= 1 tests deselected by '-khttp or quick' ================== ================== 2 passed, 1 deselected in 0.01 seconds ==================</pre> </div> </div> <div class="section" id="registering-markers"> <h2>Registering markers<a class="headerlink" href="#registering-markers" title="Permalink to this headline">¶</a></h2> <p class="versionadded"> <span class="versionmodified">New in version 2.2.</span></p> <p>Registering markers for your test suite is simple:</p> <div class="highlight-python"><pre># content of pytest.ini [pytest] markers = webtest: mark a test as a webtest.</pre> </div> <p>You can ask which markers exist for your test suite - the list includes our just defined <tt class="docutils literal"><span class="pre">webtest</span></tt> markers:</p> <div class="highlight-python"><pre>$ py.test --markers @pytest.mark.webtest: mark a test as a webtest. @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html @pytest.mark.xfail(condition, reason=None, run=True): mark the the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. See http://pytest.org/latest/skipping.html @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in multiple different argument value sets. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2. see http://pytest.org/latest/parametrize.html for more info and examples. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.</pre> </div> <p>For an example on how to add and work with markers from a plugin, see <a class="reference internal" href="#adding-a-custom-marker-from-a-plugin"><em>Custom marker and command line option to control test runs</em></a>.</p> <div class="admonition note"> <p class="first admonition-title">Note</p> <p>It is recommended to explicitely register markers so that:</p> <ul class="last simple"> <li>there is one place in your test suite defining your markers</li> <li>asking for existing markers via <tt class="docutils literal"><span class="pre">py.test</span> <span class="pre">--markers</span></tt> gives good output</li> <li>typos in function markers are treated as an error if you use the <tt class="docutils literal"><span class="pre">--strict</span></tt> option. Later versions of py.test are probably going to treat non-registered markers as an error.</li> </ul> </div> </div> <div class="section" id="marking-whole-classes-or-modules"> <span id="scoped-marking"></span><h2>Marking whole classes or modules<a class="headerlink" href="#marking-whole-classes-or-modules" title="Permalink to this headline">¶</a></h2> <p>If you are programming with Python 2.6 or later you may use <tt class="docutils literal"><span class="pre">pytest.mark</span></tt> decorators with classes to apply markers to all of its test methods:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_mark_classlevel.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="nd">@pytest.mark.webtest</span> <span class="k">class</span> <span class="nc">TestClass</span><span class="p">:</span> <span class="k">def</span> <span class="nf">test_startup</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">test_startup_and_more</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> </pre></div> </div> <p>This is equivalent to directly applying the decorator to the two test functions.</p> <p>To remain backward-compatible with Python 2.4 you can also set a <tt class="docutils literal"><span class="pre">pytestmark</span></tt> attribute on a TestClass like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pytest</span> <span class="k">class</span> <span class="nc">TestClass</span><span class="p">:</span> <span class="n">pytestmark</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">webtest</span> </pre></div> </div> <p>or if you need to use multiple markers you can use a list:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pytest</span> <span class="k">class</span> <span class="nc">TestClass</span><span class="p">:</span> <span class="n">pytestmark</span> <span class="o">=</span> <span class="p">[</span><span class="n">pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">webtest</span><span class="p">,</span> <span class="n">pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">slowtest</span><span class="p">]</span> </pre></div> </div> <p>You can also set a module level marker:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">pytest</span> <span class="n">pytestmark</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">webtest</span> </pre></div> </div> <p>in which case it will be applied to all functions and methods defined in the module.</p> </div> <div class="section" id="custom-marker-and-command-line-option-to-control-test-runs"> <span id="adding-a-custom-marker-from-a-plugin"></span><h2>Custom marker and command line option to control test runs<a class="headerlink" href="#custom-marker-and-command-line-option-to-control-test-runs" title="Permalink to this headline">¶</a></h2> <p>Plugins can provide custom markers and implement specific behaviour based on it. This is a self-contained example which adds a command line option and a parametrized test function marker to run tests specifies via named environments:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of conftest.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="k">def</span> <span class="nf">pytest_addoption</span><span class="p">(</span><span class="n">parser</span><span class="p">):</span> <span class="n">parser</span><span class="o">.</span><span class="n">addoption</span><span class="p">(</span><span class="s">"-E"</span><span class="p">,</span> <span class="n">action</span><span class="o">=</span><span class="s">"store"</span><span class="p">,</span> <span class="n">metavar</span><span class="o">=</span><span class="s">"NAME"</span><span class="p">,</span> <span class="n">help</span><span class="o">=</span><span class="s">"only run tests matching the environment NAME."</span><span class="p">)</span> <span class="k">def</span> <span class="nf">pytest_configure</span><span class="p">(</span><span class="n">config</span><span class="p">):</span> <span class="c"># register an additional marker</span> <span class="n">config</span><span class="o">.</span><span class="n">addinivalue_line</span><span class="p">(</span><span class="s">"markers"</span><span class="p">,</span> <span class="s">"env(name): mark test to run only on named environment"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">pytest_runtest_setup</span><span class="p">(</span><span class="n">item</span><span class="p">):</span> <span class="n">envmarker</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"env"</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">envmarker</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="n">envname</span> <span class="o">=</span> <span class="n">envmarker</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">if</span> <span class="n">envname</span> <span class="o">!=</span> <span class="n">item</span><span class="o">.</span><span class="n">config</span><span class="o">.</span><span class="n">getoption</span><span class="p">(</span><span class="s">"-E"</span><span class="p">):</span> <span class="n">pytest</span><span class="o">.</span><span class="n">skip</span><span class="p">(</span><span class="s">"test requires env </span><span class="si">%r</span><span class="s">"</span> <span class="o">%</span> <span class="n">envname</span><span class="p">)</span> </pre></div> </div> <p>A test file using this local plugin:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_someenv.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="nd">@pytest.mark.env</span><span class="p">(</span><span class="s">"stage1"</span><span class="p">)</span> <span class="k">def</span> <span class="nf">test_basic_db_operation</span><span class="p">():</span> <span class="k">pass</span> </pre></div> </div> <p>and an example invocations specifying a different environment than what the test needs:</p> <div class="highlight-python"><pre>$ py.test -E stage2 =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 1 items test_someenv.py s ======================== 1 skipped in 0.01 seconds =========================</pre> </div> <p>and here is one that specifies exactly the environment needed:</p> <div class="highlight-python"><pre>$ py.test -E stage1 =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 1 items test_someenv.py . ========================= 1 passed in 0.01 seconds =========================</pre> </div> <p>The <tt class="docutils literal"><span class="pre">--markers</span></tt> option always gives you a list of available markers:</p> <div class="highlight-python"><pre>$ py.test --markers @pytest.mark.env(name): mark test to run only on named environment @pytest.mark.skipif(condition): skip the given test function if eval(condition) results in a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform. see http://pytest.org/latest/skipping.html @pytest.mark.xfail(condition, reason=None, run=True): mark the the test function as an expected failure if eval(condition) has a True value. Optionally specify a reason for better reporting and run=False if you don't even want to execute the test function. See http://pytest.org/latest/skipping.html @pytest.mark.parametrize(argnames, argvalues): call a test function multiple times passing in multiple different argument value sets. Example: @parametrize('arg1', [1,2]) would lead to two calls of the decorated test function, one with arg1=1 and another with arg1=2. see http://pytest.org/latest/parametrize.html for more info and examples. @pytest.mark.usefixtures(fixturename1, fixturename2, ...): mark tests as needing all of the specified fixtures. see http://pytest.org/latest/fixture.html#usefixtures @pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible. @pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.</pre> </div> </div> <div class="section" id="reading-markers-which-were-set-from-multiple-places"> <h2>Reading markers which were set from multiple places<a class="headerlink" href="#reading-markers-which-were-set-from-multiple-places" title="Permalink to this headline">¶</a></h2> <p>If you are heavily using markers in your test suite you may encounter the case where a marker is applied several times to a test function. From plugin code you can read over all such settings. Example:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_mark_three_times.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="n">pytestmark</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">glob</span><span class="p">(</span><span class="s">"module"</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span> <span class="nd">@pytest.mark.glob</span><span class="p">(</span><span class="s">"class"</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span> <span class="k">class</span> <span class="nc">TestClass</span><span class="p">:</span> <span class="nd">@pytest.mark.glob</span><span class="p">(</span><span class="s">"function"</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span> <span class="k">def</span> <span class="nf">test_something</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span> <span class="k">pass</span> </pre></div> </div> <p>Here we have the marker “glob” applied three times to the same test function. From a conftest file we can read it like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of conftest.py</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="k">def</span> <span class="nf">pytest_runtest_setup</span><span class="p">(</span><span class="n">item</span><span class="p">):</span> <span class="n">g</span> <span class="o">=</span> <span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">"glob"</span><span class="p">,</span> <span class="bp">None</span><span class="p">)</span> <span class="k">if</span> <span class="n">g</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span> <span class="k">for</span> <span class="n">info</span> <span class="ow">in</span> <span class="n">g</span><span class="p">:</span> <span class="k">print</span> <span class="p">(</span><span class="s">"glob args=</span><span class="si">%s</span><span class="s"> kwargs=</span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span><span class="p">(</span><span class="n">info</span><span class="o">.</span><span class="n">args</span><span class="p">,</span> <span class="n">info</span><span class="o">.</span><span class="n">kwargs</span><span class="p">))</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">flush</span><span class="p">()</span> </pre></div> </div> <p>Let’s run this without capturing output and see what we get:</p> <div class="highlight-python"><pre>$ py.test -q -s glob args=('function',) kwargs={'x': 3} glob args=('class',) kwargs={'x': 2} glob args=('module',) kwargs={'x': 1} .</pre> </div> </div> <div class="section" id="marking-platform-specific-tests-with-pytest"> <h2>marking platform specific tests with pytest<a class="headerlink" href="#marking-platform-specific-tests-with-pytest" title="Permalink to this headline">¶</a></h2> <p>Consider you have a test suite which marks tests for particular platforms, namely <tt class="docutils literal"><span class="pre">pytest.mark.osx</span></tt>, <tt class="docutils literal"><span class="pre">pytest.mark.win32</span></tt> etc. and you also have tests that run on all platforms and have no specific marker. If you now want to have a way to only run the tests for your particular platform, you could use the following plugin:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of conftest.py</span> <span class="c">#</span> <span class="kn">import</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="n">ALL</span> <span class="o">=</span> <span class="nb">set</span><span class="p">(</span><span class="s">"osx linux2 win32"</span><span class="o">.</span><span class="n">split</span><span class="p">())</span> <span class="k">def</span> <span class="nf">pytest_runtest_setup</span><span class="p">(</span><span class="n">item</span><span class="p">):</span> <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">item</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">Function</span><span class="p">):</span> <span class="n">plat</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">platform</span> <span class="k">if</span> <span class="n">plat</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="p">:</span> <span class="k">if</span> <span class="n">ALL</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="p">):</span> <span class="n">pytest</span><span class="o">.</span><span class="n">skip</span><span class="p">(</span><span class="s">"cannot run on platform </span><span class="si">%s</span><span class="s">"</span> <span class="o">%</span><span class="p">(</span><span class="n">plat</span><span class="p">))</span> </pre></div> </div> <p>then tests will be skipped if they were specified for a different platform. Let’s do a little test file to show how this looks like:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_plat.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="nd">@pytest.mark.osx</span> <span class="k">def</span> <span class="nf">test_if_apple_is_evil</span><span class="p">():</span> <span class="k">pass</span> <span class="nd">@pytest.mark.linux2</span> <span class="k">def</span> <span class="nf">test_if_linux_works</span><span class="p">():</span> <span class="k">pass</span> <span class="nd">@pytest.mark.win32</span> <span class="k">def</span> <span class="nf">test_if_win32_crashes</span><span class="p">():</span> <span class="k">pass</span> <span class="k">def</span> <span class="nf">test_runs_everywhere</span><span class="p">():</span> <span class="k">pass</span> </pre></div> </div> <p>then you will see two test skipped and two executed tests as expected:</p> <div class="highlight-python"><pre>$ py.test -rs # this option reports skip reasons =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 4 items test_plat.py s.s. ========================= short test summary info ========================== SKIP [2] /tmp/doc-exec-273/conftest.py:12: cannot run on platform linux2 =================== 2 passed, 2 skipped in 0.01 seconds ====================</pre> </div> <p>Note that if you specify a platform via the marker-command line option like this:</p> <div class="highlight-python"><pre>$ py.test -m linux2 =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 4 items test_plat.py . =================== 3 tests deselected by "-m 'linux2'" ==================== ================== 1 passed, 3 deselected in 0.01 seconds ==================</pre> </div> <p>then the unmarked-tests will not be run. It is thus a way to restrict the run to the specific tests.</p> </div> <div class="section" id="automatically-adding-markers-based-on-test-names"> <h2>Automatically adding markers based on test names<a class="headerlink" href="#automatically-adding-markers-based-on-test-names" title="Permalink to this headline">¶</a></h2> <p>If you a test suite where test function names indicate a certain type of test, you can implement a hook that automatically defines markers so that you can use the <tt class="docutils literal"><span class="pre">-m</span></tt> option with it. Let’s look at this test module:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of test_module.py</span> <span class="k">def</span> <span class="nf">test_interface_simple</span><span class="p">():</span> <span class="k">assert</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">test_interface_complex</span><span class="p">():</span> <span class="k">assert</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">test_event_simple</span><span class="p">():</span> <span class="k">assert</span> <span class="mi">0</span> <span class="k">def</span> <span class="nf">test_something_else</span><span class="p">():</span> <span class="k">assert</span> <span class="mi">0</span> </pre></div> </div> <p>We want to dynamically define two markers and can do it in a <tt class="docutils literal"><span class="pre">conftest.py</span></tt> plugin:</p> <div class="highlight-python"><div class="highlight"><pre><span class="c"># content of conftest.py</span> <span class="kn">import</span> <span class="nn">pytest</span> <span class="k">def</span> <span class="nf">pytest_collection_modifyitems</span><span class="p">(</span><span class="n">items</span><span class="p">):</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">items</span><span class="p">:</span> <span class="k">if</span> <span class="s">"interface"</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">nodeid</span><span class="p">:</span> <span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="p">[</span><span class="s">"interface"</span><span class="p">]</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">interface</span> <span class="k">elif</span> <span class="s">"event"</span> <span class="ow">in</span> <span class="n">item</span><span class="o">.</span><span class="n">nodeid</span><span class="p">:</span> <span class="n">item</span><span class="o">.</span><span class="n">keywords</span><span class="p">[</span><span class="s">"event"</span><span class="p">]</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">event</span> </pre></div> </div> <p>We can now use the <tt class="docutils literal"><span class="pre">-m</span> <span class="pre">option</span></tt> to select one set:</p> <div class="highlight-python"><pre>$ py.test -m interface --tb=short =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 4 items test_module.py FF ================================= FAILURES ================================= __________________________ test_interface_simple ___________________________ test_module.py:3: in test_interface_simple > assert 0 E assert 0 __________________________ test_interface_complex __________________________ test_module.py:6: in test_interface_complex > assert 0 E assert 0 ================== 2 tests deselected by "-m 'interface'" ================== ================== 2 failed, 2 deselected in 0.01 seconds ==================</pre> </div> <p>or to select both “event” and “interface” tests:</p> <div class="highlight-python"><pre>$ py.test -m "interface or event" --tb=short =========================== test session starts ============================ platform linux2 -- Python 2.7.3 -- pytest-2.3.5 collected 4 items test_module.py FFF ================================= FAILURES ================================= __________________________ test_interface_simple ___________________________ test_module.py:3: in test_interface_simple > assert 0 E assert 0 __________________________ test_interface_complex __________________________ test_module.py:6: in test_interface_complex > assert 0 E assert 0 ____________________________ test_event_simple _____________________________ test_module.py:9: in test_event_simple > assert 0 E assert 0 ============= 1 tests deselected by "-m 'interface or event'" ============== ================== 3 failed, 1 deselected in 0.02 seconds ==================</pre> </div> </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="special.html" title="A sesssion-fixture which can look at all collected tests" >next</a></li> <li class="right" > <a href="parametrize.html" title="Parametrizing tests" >previous</a> |</li> <li><a href="../contents.html">pytest-2.3.4.1</a> »</li> <li><a href="index.html" >Usages and Examples</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>