/usr/share/doc/python-pytest/html/skipping.html is in python-pytest-doc 3.3.2-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 | <!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>Skip and xfail: dealing with tests that cannot succeed — pytest documentation</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: '3.3',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</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="search" title="Search" href="search.html" />
<link rel="next" title="Parametrizing fixtures and test functions" href="parametrize.html" />
<link rel="prev" title="Marking test functions with attributes" href="mark.html" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9">
</head>
<body>
<div class="related" role="navigation" aria-label="related navigation">
<h3>Navigation</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="parametrize.html" title="Parametrizing fixtures and test functions"
accesskey="N">next</a></li>
<li class="right" >
<a href="mark.html" title="Marking test functions with attributes"
accesskey="P">previous</a> |</li>
<li class="nav-item nav-item-0"><a href="contents.html">pytest-3.3</a> »</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="skip-and-xfail-dealing-with-tests-that-cannot-succeed">
<span id="skipping"></span><span id="skip-and-xfail"></span><h1>Skip and xfail: dealing with tests that cannot succeed<a class="headerlink" href="#skip-and-xfail-dealing-with-tests-that-cannot-succeed" title="Permalink to this headline">¶</a></h1>
<p>You can mark test functions that cannot be run on certain platforms
or that you expect to fail so pytest can deal with them accordingly and
present a summary of the test session, while keeping the test suite <em>green</em>.</p>
<p>A <strong>skip</strong> means that you expect your test to pass only if some conditions are met,
otherwise pytest should skip running the test altogether. Common examples are skipping
windows-only tests on non-windows platforms, or skipping tests that depend on an external
resource which is not available at the moment (for example a database).</p>
<p>A <strong>xfail</strong> means that you expect a test to fail for some reason.
A common example is a test for a feature not yet implemented, or a bug not yet fixed.
When a test passes despite being expected to fail (marked with <code class="docutils literal"><span class="pre">pytest.mark.xfail</span></code>),
it's an <strong>xpass</strong> and will be reported in the test summary.</p>
<p><code class="docutils literal"><span class="pre">pytest</span></code> counts and lists <em>skip</em> and <em>xfail</em> tests separately. Detailed
information about skipped/xfailed tests is not shown by default to avoid
cluttering the output. You can use the <code class="docutils literal"><span class="pre">-r</span></code> option to see details
corresponding to the "short" letters shown in the test progress:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">-</span><span class="n">rxXs</span> <span class="c1"># show extra info on xfailed, xpassed, and skipped tests</span>
</pre></div>
</div>
<p>More details on the <code class="docutils literal"><span class="pre">-r</span></code> option can be found by running <code class="docutils literal"><span class="pre">pytest</span> <span class="pre">-h</span></code>.</p>
<p>(See <a class="reference internal" href="customize.html#how-to-change-command-line-options-defaults"><span class="std std-ref">How to change command line options defaults</span></a>)</p>
<div class="section" id="skipping-test-functions">
<span id="condition-booleans"></span><span id="skip"></span><span id="skipif"></span><h2>Skipping test functions<a class="headerlink" href="#skipping-test-functions" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.9.</span></p>
</div>
<p>The simplest way to skip a test function is to mark it with the <code class="docutils literal"><span class="pre">skip</span></code> decorator
which may be passed an optional <code class="docutils literal"><span class="pre">reason</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@pytest.mark.skip</span><span class="p">(</span><span class="n">reason</span><span class="o">=</span><span class="s2">"no way of currently testing this"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_the_unknown</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>Alternatively, it is also possible to skip imperatively during test execution or setup
by calling the <code class="docutils literal"><span class="pre">pytest.skip(reason)</span></code> function:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">valid_config</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="s2">"unsupported configuration"</span><span class="p">)</span>
</pre></div>
</div>
<p>It is also possible to skip the whole module using
<code class="docutils literal"><span class="pre">pytest.skip(reason,</span> <span class="pre">allow_module_level=True)</span></code> at the module level:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pytest</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">pytest</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="s2">"--custom-flag"</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="s2">"--custom-flag is missing, skipping tests"</span><span class="p">,</span> <span class="n">allow_module_level</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
<p>The imperative method is useful when it is not possible to evaluate the skip condition
during import time.</p>
<div class="section" id="id1">
<h3><code class="docutils literal"><span class="pre">skipif</span></code><a class="headerlink" href="#id1" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.0.</span></p>
</div>
<p>If you wish to skip something conditionally then you can use <code class="docutils literal"><span class="pre">skipif</span></code> instead.
Here is an example of marking a test function to be skipped
when run on a Python3.6 interpreter:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">skipif</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o"><</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">),</span>
<span class="n">reason</span><span class="o">=</span><span class="s2">"requires python3.6"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>If the condition evaluates to <code class="docutils literal"><span class="pre">True</span></code> during collection, the test function will be skipped,
with the specified reason appearing in the summary when using <code class="docutils literal"><span class="pre">-rs</span></code>.</p>
<p>You can share <code class="docutils literal"><span class="pre">skipif</span></code> markers between modules. Consider this test module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># content of test_mymodule.py</span>
<span class="kn">import</span> <span class="nn">mymodule</span>
<span class="n">minversion</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">skipif</span><span class="p">(</span><span class="n">mymodule</span><span class="o">.</span><span class="n">__versioninfo__</span> <span class="o"><</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">1</span><span class="p">),</span>
<span class="n">reason</span><span class="o">=</span><span class="s2">"at least mymodule-1.1 required"</span><span class="p">)</span>
<span class="nd">@minversion</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>You can import the marker and reuse it in another test module:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="c1"># test_myothermodule.py</span>
<span class="kn">from</span> <span class="nn">test_mymodule</span> <span class="k">import</span> <span class="n">minversion</span>
<span class="nd">@minversion</span>
<span class="k">def</span> <span class="nf">test_anotherfunction</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>For larger test suites it's usually a good idea to have one file
where you define the markers which you then consistently apply
throughout your test suite.</p>
<p>Alternatively, you can use <a class="reference internal" href="historical-notes.html#string-conditions"><span class="std std-ref">condition strings</span></a> instead of booleans, but they can't be shared between modules easily
so they are supported mainly for backward compatibility reasons.</p>
</div>
<div class="section" id="skip-all-test-functions-of-a-class-or-module">
<h3>Skip all test functions of a class or module<a class="headerlink" href="#skip-all-test-functions-of-a-class-or-module" title="Permalink to this headline">¶</a></h3>
<p>You can use the <code class="docutils literal"><span class="pre">skipif</span></code> marker (as any other marker) on classes:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">skipif</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span> <span class="o">==</span> <span class="s1">'win32'</span><span class="p">,</span>
<span class="n">reason</span><span class="o">=</span><span class="s2">"does not run on windows"</span><span class="p">)</span>
<span class="k">class</span> <span class="nc">TestPosixCalls</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="s2">"will not be setup or run under 'win32' platform"</span>
</pre></div>
</div>
<p>If the condition is <code class="docutils literal"><span class="pre">True</span></code>, this marker will produce a skip result for
each of the test methods of that class.</p>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<p class="last">The use of <code class="docutils literal"><span class="pre">skipif</span></code> on classes that use inheritance is strongly
discouraged. <a class="reference external" href="https://github.com/pytest-dev/pytest/issues/568">A Known bug</a>
in pytest's markers may cause unexpected behavior in super classes.</p>
</div>
<p>If you want to skip all test functions of a module, you may use
the <code class="docutils literal"><span class="pre">pytestmark</span></code> name on the global level:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="c1"># test_module.py</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">skipif</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>If multiple <code class="docutils literal"><span class="pre">skipif</span></code> decorators are applied to a test function, it
will be skipped if any of the skip conditions is true.</p>
</div>
<div class="section" id="skipping-files-or-directories">
<h3>Skipping files or directories<a class="headerlink" href="#skipping-files-or-directories" title="Permalink to this headline">¶</a></h3>
<p>Sometimes you may need to skip an entire file or directory, for example if the
tests rely on Python version-specific features or contain code that you do not
wish pytest to run. In this case, you must exclude the files and directories
from collection. Refer to <a class="reference internal" href="example/pythoncollection.html#customizing-test-collection"><span class="std std-ref">Customizing test collection</span></a> for more
information.</p>
</div>
<div class="section" id="skipping-on-a-missing-import-dependency">
<h3>Skipping on a missing import dependency<a class="headerlink" href="#skipping-on-a-missing-import-dependency" title="Permalink to this headline">¶</a></h3>
<p>You can use the following helper at module level
or within a test or test setup function:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">docutils</span> <span class="o">=</span> <span class="n">pytest</span><span class="o">.</span><span class="n">importorskip</span><span class="p">(</span><span class="s2">"docutils"</span><span class="p">)</span>
</pre></div>
</div>
<p>If <code class="docutils literal"><span class="pre">docutils</span></code> cannot be imported here, this will lead to a
skip outcome of the test. You can also skip based on the
version number of a library:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">docutils</span> <span class="o">=</span> <span class="n">pytest</span><span class="o">.</span><span class="n">importorskip</span><span class="p">(</span><span class="s2">"docutils"</span><span class="p">,</span> <span class="n">minversion</span><span class="o">=</span><span class="s2">"0.3"</span><span class="p">)</span>
</pre></div>
</div>
<p>The version will be read from the specified
module's <code class="docutils literal"><span class="pre">__version__</span></code> attribute.</p>
</div>
<div class="section" id="summary">
<h3>Summary<a class="headerlink" href="#summary" title="Permalink to this headline">¶</a></h3>
<p>Here's a quick guide on how to skip tests in a module in different situations:</p>
<ol class="arabic simple">
<li>Skip all tests in a module unconditionally:</li>
</ol>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></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">skip</span><span class="p">(</span><span class="s1">'all tests still WIP'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<ol class="arabic simple" start="2">
<li>Skip all tests in a module based on some condition:</li>
</ol>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></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">skipif</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">platform</span> <span class="o">==</span> <span class="s1">'win32'</span><span class="p">,</span> <span class="s1">'tests for linux only'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
<ol class="arabic simple" start="3">
<li>Skip all tests in a module if some import is missing:</li>
</ol>
<blockquote>
<div><div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">pexpect</span> <span class="o">=</span> <span class="n">pytest</span><span class="o">.</span><span class="n">importorskip</span><span class="p">(</span><span class="s1">'pexpect'</span><span class="p">)</span>
</pre></div>
</div>
</div></blockquote>
</div>
</div>
<div class="section" id="xfail-mark-test-functions-as-expected-to-fail">
<span id="xfail"></span><h2>XFail: mark test functions as expected to fail<a class="headerlink" href="#xfail-mark-test-functions-as-expected-to-fail" title="Permalink to this headline">¶</a></h2>
<p>You can use the <code class="docutils literal"><span class="pre">xfail</span></code> marker to indicate that you
expect a test to fail:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">xfail</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This test will be run but no traceback will be reported
when it fails. Instead terminal reporting will list it in the
"expected to fail" (<code class="docutils literal"><span class="pre">XFAIL</span></code>) or "unexpectedly passing" (<code class="docutils literal"><span class="pre">XPASS</span></code>) sections.</p>
<p>Alternatively, you can also mark a test as <code class="docutils literal"><span class="pre">XFAIL</span></code> from within a test or setup function
imperatively:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">valid_config</span><span class="p">():</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">xfail</span><span class="p">(</span><span class="s2">"failing configuration (but should work)"</span><span class="p">)</span>
</pre></div>
</div>
<p>This will unconditionally make <code class="docutils literal"><span class="pre">test_function</span></code> <code class="docutils literal"><span class="pre">XFAIL</span></code>. Note that no other code is executed
after <code class="docutils literal"><span class="pre">pytest.xfail</span></code> call, differently from the marker. That's because it is implemented
internally by raising a known exception.</p>
<p>Here's the signature of the <code class="docutils literal"><span class="pre">xfail</span></code> <strong>marker</strong> (not the function), using Python 3 keyword-only
arguments syntax:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">xfail</span><span class="p">(</span><span class="n">condition</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">reason</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">raises</span><span class="o">=</span><span class="bp">None</span><span class="p">,</span> <span class="n">run</span><span class="o">=</span><span class="bp">True</span><span class="p">,</span> <span class="n">strict</span><span class="o">=</span><span class="bp">False</span><span class="p">):</span>
</pre></div>
</div>
<div class="section" id="strict-parameter">
<h3><code class="docutils literal"><span class="pre">strict</span></code> parameter<a class="headerlink" href="#strict-parameter" title="Permalink to this headline">¶</a></h3>
<div class="versionadded">
<p><span class="versionmodified">New in version 2.9.</span></p>
</div>
<p>Both <code class="docutils literal"><span class="pre">XFAIL</span></code> and <code class="docutils literal"><span class="pre">XPASS</span></code> don't fail the test suite, unless the <code class="docutils literal"><span class="pre">strict</span></code> keyword-only
parameter is passed as <code class="docutils literal"><span class="pre">True</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@pytest.mark.xfail</span><span class="p">(</span><span class="n">strict</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This will make <code class="docutils literal"><span class="pre">XPASS</span></code> ("unexpectedly passing") results from this test to fail the test suite.</p>
<p>You can change the default value of the <code class="docutils literal"><span class="pre">strict</span></code> parameter using the
<code class="docutils literal"><span class="pre">xfail_strict</span></code> ini option:</p>
<div class="highlight-ini"><div class="highlight"><pre><span></span><span class="k">[pytest]</span>
<span class="na">xfail_strict</span><span class="o">=</span><span class="s">true</span>
</pre></div>
</div>
</div>
<div class="section" id="reason-parameter">
<h3><code class="docutils literal"><span class="pre">reason</span></code> parameter<a class="headerlink" href="#reason-parameter" title="Permalink to this headline">¶</a></h3>
<p>As with <a class="reference internal" href="#skipif">skipif</a> you can also mark your expectation of a failure
on a particular platform:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">xfail</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">>=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span><span class="mi">6</span><span class="p">),</span>
<span class="n">reason</span><span class="o">=</span><span class="s2">"python3.6 api changes"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
</div>
<div class="section" id="raises-parameter">
<h3><code class="docutils literal"><span class="pre">raises</span></code> parameter<a class="headerlink" href="#raises-parameter" title="Permalink to this headline">¶</a></h3>
<p>If you want to be more specific as to why the test is failing, you can specify
a single exception, or a list of exceptions, in the <code class="docutils literal"><span class="pre">raises</span></code> argument.</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@pytest.mark.xfail</span><span class="p">(</span><span class="n">raises</span><span class="o">=</span><span class="ne">RuntimeError</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>Then the test will be reported as a regular failure if it fails with an
exception not mentioned in <code class="docutils literal"><span class="pre">raises</span></code>.</p>
</div>
<div class="section" id="run-parameter">
<h3><code class="docutils literal"><span class="pre">run</span></code> parameter<a class="headerlink" href="#run-parameter" title="Permalink to this headline">¶</a></h3>
<p>If a test should be marked as xfail and reported as such but should not be
even executed, use the <code class="docutils literal"><span class="pre">run</span></code> parameter as <code class="docutils literal"><span class="pre">False</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="nd">@pytest.mark.xfail</span><span class="p">(</span><span class="n">run</span><span class="o">=</span><span class="bp">False</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_function</span><span class="p">():</span>
<span class="o">...</span>
</pre></div>
</div>
<p>This is specially useful for xfailing tests that are crashing the interpreter and should be
investigated later.</p>
</div>
<div class="section" id="ignoring-xfail">
<h3>Ignoring xfail<a class="headerlink" href="#ignoring-xfail" title="Permalink to this headline">¶</a></h3>
<p>By specifying on the commandline:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="n">pytest</span> <span class="o">--</span><span class="n">runxfail</span>
</pre></div>
</div>
<p>you can force the running and reporting of an <code class="docutils literal"><span class="pre">xfail</span></code> marked test
as if it weren't marked at all. This also causes <code class="docutils literal"><span class="pre">pytest.xfail</span></code> to produce no effect.</p>
</div>
<div class="section" id="examples">
<h3>Examples<a class="headerlink" href="#examples" title="Permalink to this headline">¶</a></h3>
<p>Here is a simple test file with the several usages:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pytest</span>
<span class="n">xfail</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">xfail</span>
<span class="nd">@xfail</span>
<span class="k">def</span> <span class="nf">test_hello</span><span class="p">():</span>
<span class="k">assert</span> <span class="mi">0</span>
<span class="nd">@xfail</span><span class="p">(</span><span class="n">run</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_hello2</span><span class="p">():</span>
<span class="k">assert</span> <span class="mi">0</span>
<span class="nd">@xfail</span><span class="p">(</span><span class="s2">"hasattr(os, 'sep')"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_hello3</span><span class="p">():</span>
<span class="k">assert</span> <span class="mi">0</span>
<span class="nd">@xfail</span><span class="p">(</span><span class="n">reason</span><span class="o">=</span><span class="s2">"bug 110"</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_hello4</span><span class="p">():</span>
<span class="k">assert</span> <span class="mi">0</span>
<span class="nd">@xfail</span><span class="p">(</span><span class="s1">'pytest.__version__[0] != "17"'</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_hello5</span><span class="p">():</span>
<span class="k">assert</span> <span class="mi">0</span>
<span class="k">def</span> <span class="nf">test_hello6</span><span class="p">():</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">xfail</span><span class="p">(</span><span class="s2">"reason"</span><span class="p">)</span>
<span class="nd">@xfail</span><span class="p">(</span><span class="n">raises</span><span class="o">=</span><span class="ne">IndexError</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test_hello7</span><span class="p">():</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">x</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
</pre></div>
</div>
<p>Running it with the report-on-xfail option gives this output:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>example $ pytest -rx xfail_demo.py
=========================== test session starts ============================
platform linux -- Python 3.x.y, pytest-3.x.y, py-1.x.y, pluggy-0.x.y
rootdir: $REGENDOC_TMPDIR/example, inifile:
collected 7 items
xfail_demo.py xxxxxxx [100%]
========================= short test summary info ==========================
XFAIL xfail_demo.py::test_hello
XFAIL xfail_demo.py::test_hello2
reason: [NOTRUN]
XFAIL xfail_demo.py::test_hello3
condition: hasattr(os, 'sep')
XFAIL xfail_demo.py::test_hello4
bug 110
XFAIL xfail_demo.py::test_hello5
condition: pytest.__version__[0] != "17"
XFAIL xfail_demo.py::test_hello6
reason: reason
XFAIL xfail_demo.py::test_hello7
======================== 7 xfailed in 0.12 seconds =========================
</pre></div>
</div>
</div>
</div>
<div class="section" id="skip-xfail-with-parametrize">
<span id="id2"></span><h2>Skip/xfail with parametrize<a class="headerlink" href="#skip-xfail-with-parametrize" title="Permalink to this headline">¶</a></h2>
<p>It is possible to apply markers like skip and xfail to individual
test instances when using parametrize:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pytest</span>
<span class="nd">@pytest.mark.parametrize</span><span class="p">((</span><span class="s2">"n"</span><span class="p">,</span> <span class="s2">"expected"</span><span class="p">),</span> <span class="p">[</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">),</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">param</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">marks</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">xfail</span><span class="p">),</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">param</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="n">marks</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">xfail</span><span class="p">(</span><span class="n">reason</span><span class="o">=</span><span class="s2">"some bug"</span><span class="p">)),</span>
<span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">),</span>
<span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">),</span>
<span class="n">pytest</span><span class="o">.</span><span class="n">param</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="n">marks</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">skipif</span><span class="p">(</span><span class="n">sys</span><span class="o">.</span><span class="n">version_info</span> <span class="o">>=</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">),</span> <span class="n">reason</span><span class="o">=</span><span class="s2">"py2k"</span><span class="p">)),</span>
<span class="p">])</span>
<span class="k">def</span> <span class="nf">test_increment</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">expected</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">n</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">==</span> <span class="n">expected</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<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>
<li><a href="backwards-compatibility.html">Backwards Compatibility</a></li>
<li><a href="license.html">License</a></li>
</ul>
<hr>
<ul>
<li><a class="reference internal" href="#">Skip and xfail: dealing with tests that cannot succeed</a><ul>
<li><a class="reference internal" href="#skipping-test-functions">Skipping test functions</a><ul>
<li><a class="reference internal" href="#id1"><code class="docutils literal"><span class="pre">skipif</span></code></a></li>
<li><a class="reference internal" href="#skip-all-test-functions-of-a-class-or-module">Skip all test functions of a class or module</a></li>
<li><a class="reference internal" href="#skipping-files-or-directories">Skipping files or directories</a></li>
<li><a class="reference internal" href="#skipping-on-a-missing-import-dependency">Skipping on a missing import dependency</a></li>
<li><a class="reference internal" href="#summary">Summary</a></li>
</ul>
</li>
<li><a class="reference internal" href="#xfail-mark-test-functions-as-expected-to-fail">XFail: mark test functions as expected to fail</a><ul>
<li><a class="reference internal" href="#strict-parameter"><code class="docutils literal"><span class="pre">strict</span></code> parameter</a></li>
<li><a class="reference internal" href="#reason-parameter"><code class="docutils literal"><span class="pre">reason</span></code> parameter</a></li>
<li><a class="reference internal" href="#raises-parameter"><code class="docutils literal"><span class="pre">raises</span></code> parameter</a></li>
<li><a class="reference internal" href="#run-parameter"><code class="docutils literal"><span class="pre">run</span></code> parameter</a></li>
<li><a class="reference internal" href="#ignoring-xfail">Ignoring xfail</a></li>
<li><a class="reference internal" href="#examples">Examples</a></li>
</ul>
</li>
<li><a class="reference internal" href="#skip-xfail-with-parametrize">Skip/xfail with parametrize</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li>Previous: <a href="mark.html" title="previous chapter">Marking test functions with attributes</a></li>
<li>Next: <a href="parametrize.html" title="next chapter">Parametrizing fixtures and test functions</a></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://github.com/pytest-dev/pytest/">pytest @ GitHub</a></li>
<li><a href="http://plugincompat.herokuapp.com/">3rd party plugins</a></li>
<li><a href="https://github.com/pytest-dev/pytest/issues">Issue Tracker</a></li>
<li><a href="https://media.readthedocs.org/pdf/pytest/latest/pytest.pdf">PDF Documentation</a>
</ul>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<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>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
© Copyright 2018, holger krekel and pytest-dev team.
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a>.
</div>
</body>
</html>
|