1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<meta content="text/html;charset=ISO-8859-1" name="Content-Type"/>
6
<link href="../../style.css" media="screen" rel="stylesheet" type="text/css"/></head>
9
<div><a href="http://pylib.org"><img alt="py lib" height="57" id="pyimg" src="http://codespeak.net/img/pylib.png" width="77"/></a></div>
11
<div style="font-style: italic;">trunk</div>
13
<div><a class="menu" href="../../download.html">install</a></div></div>
15
<div><a class="menu" href="../../contact.html">contact</a></div></div>
17
<div><a class="menu" href="../../changelog.html">changelog</a></div></div>
19
<div><a class="menu" href="../../faq.html">faq</a></div></div>
23
<div><a class="menu" href="../index.html">doc index</a></div>
24
<div><a class="menu" href="../features.html">features</a></div>
25
<div><a class="menu" href="../quickstart.html">quickstart</a></div>
26
<div><a class="menu" href="../talks.html">tutorials</a></div>
27
<div><a class="menu" href="index.html">plugins</a></div>
28
<div><a class="menu" href="../funcargs.html">funcargs</a></div>
29
<div><a class="menu" href="../customize.html">customize</a></div></div></div>
32
<h3>supporting APIs:</h3>
33
<div><a class="menu" href="../../index.html">pylib index</a></div>
34
<div><a class="menu" href="../../execnet.html">py.execnet</a></div>
35
<div><a class="menu" href="../../path.html">py.path</a></div>
36
<div><a class="menu" href="../../code.html">py.code</a></div></div></div></div></div>
37
<div id="contentspace">
40
<div class="section" id="pytest-xfail-plugin">
41
<h1>pytest_xfail plugin</h1>
42
<p>mark python test functions, classes or modules for conditional</p>
43
<div class="contents local topic" id="contents">
45
<li><a class="reference internal" href="#skip-a-test-function-conditionally" id="id1">skip a test function conditionally</a></li>
46
<li><a class="reference internal" href="#conditionally-mark-a-function-as-expected-to-fail" id="id2">conditionally mark a function as "expected to fail"</a></li>
47
<li><a class="reference internal" href="#skip-xfail-a-whole-test-class-or-module" id="id3">skip/xfail a whole test class or module</a></li>
48
<li><a class="reference internal" href="#skip-if-a-dependency-cannot-be-imported" id="id4">skip if a dependency cannot be imported</a></li>
49
<li><a class="reference internal" href="#dynamically-skip-from-within-a-test-or-setup" id="id5">dynamically skip from within a test or setup</a></li>
52
<p>skipping (skipif) or as expected-to-fail (xfail). Both declarations
53
lead to special reporting and both can be systematically associated
54
with functions, whole classes or modules. The difference between
55
the two is that 'xfail' will still execute test functions
56
but it will revert the outcome. A passing test is now
57
a failure and failing test is expected. All skip conditions
58
are reported at the end of test run through the terminal
60
<div class="section" id="skip-a-test-function-conditionally">
61
<span id="skipif"></span><h2><a class="toc-backref" href="#id1">skip a test function conditionally</a></h2>
62
<p>Here is an example for skipping a test function on Python3:</p>
63
<pre class="literal-block">
64
@py.test.mark.skipif("sys.version_info >= (3,0)")
68
<p>Conditions are specified as python expressions
69
and can access the <tt class="docutils literal"><span class="pre">sys</span></tt> module. They can also
70
access the config object and thus depend on command
71
line or conftest options:</p>
72
<pre class="literal-block">
73
@py.test.mark.skipif("config.getvalue('db') is None")
74
def test_function(...):
78
<div class="section" id="conditionally-mark-a-function-as-expected-to-fail">
79
<h2><a class="toc-backref" href="#id2">conditionally mark a function as "expected to fail"</a></h2>
80
<p>You can use the <tt class="docutils literal"><span class="pre">xfail</span></tt> keyword to mark your test functions as
81
'expected to fail':</p>
82
<pre class="literal-block">
83
@py.test.mark.xfail
87
<p>This test will be executed but no traceback will be reported
88
when it fails. Instead terminal reporting will list it in the
89
"expected to fail" or "unexpectedly passing" sections.
90
As with <a class="reference internal" href="#skipif">skipif</a> you may selectively expect a failure
91
depending on platform:</p>
92
<pre class="literal-block">
93
@py.test.mark.xfail("sys.version_info >= (3,0)")
98
<div class="section" id="skip-xfail-a-whole-test-class-or-module">
99
<h2><a class="toc-backref" href="#id3">skip/xfail a whole test class or module</a></h2>
100
<p>Instead of marking single functions you can skip
101
a whole class of tests when runnign on a specific
103
<pre class="literal-block">
105
skipif = "sys.platform == 'win32'"
107
<p>Or you can mark all test functions as expected
108
to fail for a specific test configuration:</p>
109
<pre class="literal-block">
110
xfail = "config.getvalue('db') == 'mysql'"
113
<div class="section" id="skip-if-a-dependency-cannot-be-imported">
114
<h2><a class="toc-backref" href="#id4">skip if a dependency cannot be imported</a></h2>
115
<p>You can use a helper to skip on a failing import:</p>
116
<pre class="literal-block">
117
docutils = py.test.importorskip("docutils")
119
<p>You can use this helper at module level or within
120
a test or setup function.</p>
121
<p>You can aslo skip if a library does not have the right version:</p>
122
<pre class="literal-block">
123
docutils = py.test.importorskip("docutils", minversion="0.3")
125
<p>The version will be read from the specified module's <tt class="docutils literal"><span class="pre">__version__</span></tt> attribute.</p>
127
<div class="section" id="dynamically-skip-from-within-a-test-or-setup">
128
<h2><a class="toc-backref" href="#id5">dynamically skip from within a test or setup</a></h2>
129
<p>If you want to skip the execution of a test you can call
130
<tt class="docutils literal"><span class="pre">py.test.skip()</span></tt> within a test, a setup or from a
131
<a class="reference external" href="../funcargs.html#factory">funcarg factory</a> function. Example:</p>
132
<pre class="literal-block">
134
if not valid_config():
135
py.test.skip("unsuppored configuration")
139
<div class="section" id="start-improving-this-plugin-in-30-seconds">
140
<h1>Start improving this plugin in 30 seconds</h1>
141
<ol class="arabic simple">
142
<li>Download <a class="reference external" href="http://bitbucket.org/hpk42/py-trunk/raw/1.1.0b1/py/test/plugin/pytest_xfail.py">pytest_xfail.py</a> plugin source code</li>
143
<li>put it somewhere as <tt class="docutils literal"><span class="pre">pytest_xfail.py</span></tt> into your import path</li>
144
<li>a subsequent <tt class="docutils literal"><span class="pre">py.test</span></tt> run will use your local version</li>
146
<p>Checkout <a class="reference external" href="../customize.html">customize</a>, other <a class="reference external" href="index.html">plugins</a> or <a class="reference external" href="../../contact.html">get in contact</a>.</p>
149
<script type="text/javascript">
150
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
151
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
153
<script type="text/javascript">
155
var pageTracker = _gat._getTracker("UA-7597274-3");
156
pageTracker._trackPageview();
157
} catch(err) {}</script>
b'\\ No newline at end of file'