/usr/share/doc/python-pytest/html/builtin.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 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 | <!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>Pytest API and builtin fixtures — 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="pytest fixtures: explicit, modular, scalable" href="fixture.html" />
<link rel="prev" title="The writing and reporting of assertions in tests" href="assert.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="fixture.html" title="pytest fixtures: explicit, modular, scalable"
accesskey="N">next</a></li>
<li class="right" >
<a href="assert.html" title="The writing and reporting of assertions in tests"
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="pytest-api-and-builtin-fixtures">
<span id="pytest-helpers"></span><h1>Pytest API and builtin fixtures<a class="headerlink" href="#pytest-api-and-builtin-fixtures" title="Permalink to this headline">¶</a></h1>
<p>This is a list of <code class="docutils literal"><span class="pre">pytest.*</span></code> API functions and fixtures.</p>
<p>For information on plugin hooks and objects, see <a class="reference internal" href="writing_plugins.html#plugins"><span class="std std-ref">Writing plugins</span></a>.</p>
<p>For information on the <code class="docutils literal"><span class="pre">pytest.mark</span></code> mechanism, see <a class="reference internal" href="mark.html#mark"><span class="std std-ref">Marking test functions with attributes</span></a>.</p>
<p>For the below objects, you can also interactively ask for help, e.g. by
typing on the Python interactive prompt something like:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">pytest</span>
<span class="n">help</span><span class="p">(</span><span class="n">pytest</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="invoking-pytest-interactively">
<h2>Invoking pytest interactively<a class="headerlink" href="#invoking-pytest-interactively" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="pytest.main">
<code class="descname">main</code><span class="sig-paren">(</span><em>args=None</em>, <em>plugins=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/config.html#main"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pytest.main" title="Permalink to this definition">¶</a></dt>
<dd><p>return exit code, after performing an in-process test run.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>args</strong> -- list of command line arguments.</li>
<li><strong>plugins</strong> -- list of plugin objects to be auto-registered during
initialization.</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<p>More examples at <a class="reference internal" href="usage.html#pytest-main-usage"><span class="std std-ref">Calling pytest from Python code</span></a></p>
</div>
<div class="section" id="helpers-for-assertions-about-exceptions-warnings">
<h2>Helpers for assertions about Exceptions/Warnings<a class="headerlink" href="#helpers-for-assertions-about-exceptions-warnings" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="pytest.raises">
<code class="descname">raises</code><span class="sig-paren">(</span><em>expected_exception</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/python_api.html#raises"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pytest.raises" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that a code block/function call raises <code class="docutils literal"><span class="pre">expected_exception</span></code>
and raise a failure exception otherwise.</p>
<p>This helper produces a <code class="docutils literal"><span class="pre">ExceptionInfo()</span></code> object (see below).</p>
<p>You may use this function as a context manager:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">):</span>
<span class="gp">... </span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span>
</pre></div>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 2.10.</span></p>
</div>
<p>In the context manager form you may use the keyword argument
<code class="docutils literal"><span class="pre">message</span></code> to specify a custom failure message:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="n">message</span><span class="o">=</span><span class="s2">"Expecting ZeroDivisionError"</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">pass</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">Failed</span>: <span class="n">Expecting ZeroDivisionError</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>When using <code class="docutils literal"><span class="pre">pytest.raises</span></code> as a context manager, it's worthwhile to
note that normal context manager rules apply and that the exception
raised <em>must</em> be the final line in the scope of the context manager.
Lines of code after that, within the scope of the context manager will
not be executed. For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">value</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc_info</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">value</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"value must be <= 10"</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">assert</span> <span class="n">exc_info</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="ne">ValueError</span> <span class="c1"># this will not execute</span>
</pre></div>
</div>
<p>Instead, the following approach must be taken (note the difference in
scope):</p>
<div class="last highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">)</span> <span class="k">as</span> <span class="n">exc_info</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">if</span> <span class="n">value</span> <span class="o">></span> <span class="mi">10</span><span class="p">:</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"value must be <= 10"</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="k">assert</span> <span class="n">exc_info</span><span class="o">.</span><span class="n">type</span> <span class="o">==</span> <span class="ne">ValueError</span>
</pre></div>
</div>
</div>
<p>Since version <code class="docutils literal"><span class="pre">3.1</span></code> you can use the keyword argument <code class="docutils literal"><span class="pre">match</span></code> to assert that the
exception matches a text or regex:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="s1">'must be 0 or None'</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"value must be 0 or None"</span><span class="p">)</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">raises</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">,</span> <span class="n">match</span><span class="o">=</span><span class="sa">r</span><span class="s1">'must be \d+$'</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">"value must be 42"</span><span class="p">)</span>
</pre></div>
</div>
<p><strong>Legacy forms</strong></p>
<p>The forms below are fully supported but are discouraged for new code because the
context manager form is regarded as more readable and less error-prone.</p>
<p>It is possible to specify a callable by passing a to-be-called lambda:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="k">lambda</span><span class="p">:</span> <span class="mi">1</span><span class="o">/</span><span class="mi">0</span><span class="p">)</span>
<span class="go"><ExceptionInfo ...></span>
</pre></div>
</div>
<p>or you can specify an arbitrary callable with arguments:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="n">x</span><span class="p">):</span> <span class="k">return</span> <span class="mi">1</span><span class="o">/</span><span class="n">x</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go"><ExceptionInfo ...></span>
<span class="gp">>>> </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="n">f</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="go"><ExceptionInfo ...></span>
</pre></div>
</div>
<p>It is also possible to pass a string to be evaluated at runtime:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">raises</span><span class="p">(</span><span class="ne">ZeroDivisionError</span><span class="p">,</span> <span class="s2">"f(0)"</span><span class="p">)</span>
<span class="go"><ExceptionInfo ...></span>
</pre></div>
</div>
<p>The string will be evaluated using the same <code class="docutils literal"><span class="pre">locals()</span></code> and <code class="docutils literal"><span class="pre">globals()</span></code>
at the moment of the <code class="docutils literal"><span class="pre">raises</span></code> call.</p>
<dl class="class">
<dt id="_pytest._code.ExceptionInfo">
<em class="property">class </em><code class="descname">ExceptionInfo</code><span class="sig-paren">(</span><em>tup=None</em>, <em>exprinfo=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/_code/code.html#ExceptionInfo"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest._code.ExceptionInfo" title="Permalink to this definition">¶</a></dt>
<dd><p>wraps sys.exc_info() objects and offers
help for navigating the traceback.</p>
<dl class="attribute">
<dt id="_pytest._code.ExceptionInfo.type">
<code class="descname">type</code><em class="property"> = None</em><a class="headerlink" href="#_pytest._code.ExceptionInfo.type" title="Permalink to this definition">¶</a></dt>
<dd><p>the exception class</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest._code.ExceptionInfo.value">
<code class="descname">value</code><em class="property"> = None</em><a class="headerlink" href="#_pytest._code.ExceptionInfo.value" title="Permalink to this definition">¶</a></dt>
<dd><p>the exception instance</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest._code.ExceptionInfo.tb">
<code class="descname">tb</code><em class="property"> = None</em><a class="headerlink" href="#_pytest._code.ExceptionInfo.tb" title="Permalink to this definition">¶</a></dt>
<dd><p>the exception raw traceback</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest._code.ExceptionInfo.typename">
<code class="descname">typename</code><em class="property"> = None</em><a class="headerlink" href="#_pytest._code.ExceptionInfo.typename" title="Permalink to this definition">¶</a></dt>
<dd><p>the exception type name</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest._code.ExceptionInfo.traceback">
<code class="descname">traceback</code><em class="property"> = None</em><a class="headerlink" href="#_pytest._code.ExceptionInfo.traceback" title="Permalink to this definition">¶</a></dt>
<dd><p>the exception traceback (_pytest._code.Traceback instance)</p>
</dd></dl>
<dl class="method">
<dt id="_pytest._code.ExceptionInfo.exconly">
<code class="descname">exconly</code><span class="sig-paren">(</span><em>tryshort=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/_code/code.html#ExceptionInfo.exconly"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest._code.ExceptionInfo.exconly" title="Permalink to this definition">¶</a></dt>
<dd><p>return the exception as a string</p>
<p>when 'tryshort' resolves to True, and the exception is a
_pytest._code._AssertionError, only the actual exception part of
the exception representation is returned (so 'AssertionError: ' is
removed from the beginning)</p>
</dd></dl>
<dl class="method">
<dt id="_pytest._code.ExceptionInfo.errisinstance">
<code class="descname">errisinstance</code><span class="sig-paren">(</span><em>exc</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/_code/code.html#ExceptionInfo.errisinstance"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest._code.ExceptionInfo.errisinstance" title="Permalink to this definition">¶</a></dt>
<dd><p>return True if the exception is an instance of exc</p>
</dd></dl>
<dl class="method">
<dt id="_pytest._code.ExceptionInfo.getrepr">
<code class="descname">getrepr</code><span class="sig-paren">(</span><em>showlocals=False</em>, <em>style='long'</em>, <em>abspath=False</em>, <em>tbfilter=True</em>, <em>funcargs=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/_code/code.html#ExceptionInfo.getrepr"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest._code.ExceptionInfo.getrepr" title="Permalink to this definition">¶</a></dt>
<dd><p>return str()able representation of this exception info.
showlocals: show locals per traceback entry
style: long|short|no|native traceback style
tbfilter: hide entries (where __tracebackhide__ is true)</p>
<p>in case of style==native, tbfilter and showlocals is ignored.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest._code.ExceptionInfo.match">
<code class="descname">match</code><span class="sig-paren">(</span><em>regexp</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/_code/code.html#ExceptionInfo.match"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest._code.ExceptionInfo.match" title="Permalink to this definition">¶</a></dt>
<dd><p>Match the regular expression 'regexp' on the string representation of
the exception. If it matches then True is returned (so that it is
possible to write 'assert excinfo.match()'). If it doesn't match an
AssertionError is raised.</p>
</dd></dl>
</dd></dl>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p>Similar to caught exception objects in Python, explicitly clearing
local references to returned <code class="docutils literal"><span class="pre">ExceptionInfo</span></code> objects can
help the Python interpreter speed up its garbage collection.</p>
<p class="last">Clearing those references breaks a reference cycle
(<code class="docutils literal"><span class="pre">ExceptionInfo</span></code> --> caught exception --> frame stack raising
the exception --> current frame stack --> local variables -->
<code class="docutils literal"><span class="pre">ExceptionInfo</span></code>) which makes Python keep all objects referenced
from that cycle (including all local variables in the current
frame) alive until the next cyclic garbage collection run. See the
official Python <code class="docutils literal"><span class="pre">try</span></code> statement documentation for more detailed
information.</p>
</div>
</dd></dl>
<p>Examples at <a class="reference internal" href="assert.html#assertraises"><span class="std std-ref">Assertions about expected exceptions</span></a>.</p>
<dl class="function">
<dt id="pytest.deprecated_call">
<code class="descname">deprecated_call</code><span class="sig-paren">(</span><em>func=None</em>, <em>*args</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/recwarn.html#deprecated_call"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pytest.deprecated_call" title="Permalink to this definition">¶</a></dt>
<dd><p>context manager that can be used to ensure a block of code triggers a
<code class="docutils literal"><span class="pre">DeprecationWarning</span></code> or <code class="docutils literal"><span class="pre">PendingDeprecationWarning</span></code>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">warnings</span>
<span class="gp">>>> </span><span class="k">def</span> <span class="nf">api_call_v2</span><span class="p">():</span>
<span class="gp">... </span> <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">'use v3 of this api'</span><span class="p">,</span> <span class="ne">DeprecationWarning</span><span class="p">)</span>
<span class="gp">... </span> <span class="k">return</span> <span class="mi">200</span>
<span class="gp">>>> </span><span class="k">with</span> <span class="n">deprecated_call</span><span class="p">():</span>
<span class="gp">... </span> <span class="k">assert</span> <span class="n">api_call_v2</span><span class="p">()</span> <span class="o">==</span> <span class="mi">200</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">deprecated_call</span></code> can also be used by passing a function and <code class="docutils literal"><span class="pre">*args</span></code> and <code class="docutils literal"><span class="pre">*kwargs</span></code>,
in which case it will ensure calling <code class="docutils literal"><span class="pre">func(*args,</span> <span class="pre">**kwargs)</span></code> produces one of the warnings
types above.</p>
</dd></dl>
</div>
<div class="section" id="comparing-floating-point-numbers">
<h2>Comparing floating point numbers<a class="headerlink" href="#comparing-floating-point-numbers" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="pytest.approx">
<code class="descname">approx</code><span class="sig-paren">(</span><em>expected</em>, <em>rel=None</em>, <em>abs=None</em>, <em>nan_ok=False</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/python_api.html#approx"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pytest.approx" title="Permalink to this definition">¶</a></dt>
<dd><p>Assert that two numbers (or two sets of numbers) are equal to each other
within some tolerance.</p>
<p>Due to the <a class="reference external" href="https://docs.python.org/3/tutorial/floatingpoint.html">intricacies of floating-point arithmetic</a>, numbers that we
would intuitively expect to be equal are not always so:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span> <span class="o">==</span> <span class="mf">0.3</span>
<span class="go">False</span>
</pre></div>
</div>
<p>This problem is commonly encountered when writing tests, e.g. when making
sure that floating-point values are what you expect them to be. One way to
deal with this problem is to assert that two floating-point numbers are
equal to within some appropriate tolerance:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="nb">abs</span><span class="p">((</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">)</span> <span class="o">-</span> <span class="mf">0.3</span><span class="p">)</span> <span class="o"><</span> <span class="mf">1e-6</span>
<span class="go">True</span>
</pre></div>
</div>
<p>However, comparisons like this are tedious to write and difficult to
understand. Furthermore, absolute comparisons like the one above are
usually discouraged because there's no tolerance that works well for all
situations. <code class="docutils literal"><span class="pre">1e-6</span></code> is good for numbers around <code class="docutils literal"><span class="pre">1</span></code>, but too small for
very big numbers and too big for very small ones. It's better to express
the tolerance as a fraction of the expected value, but relative comparisons
like that are even more difficult to write correctly and concisely.</p>
<p>The <code class="docutils literal"><span class="pre">approx</span></code> class performs floating-point comparisons using a syntax
that's as intuitive as possible:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">pytest</span> <span class="k">import</span> <span class="n">approx</span>
<span class="gp">>>> </span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mf">0.3</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>The same syntax also works for sequences of numbers:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">(</span><span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.2</span> <span class="o">+</span> <span class="mf">0.4</span><span class="p">)</span> <span class="o">==</span> <span class="n">approx</span><span class="p">((</span><span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>Dictionary <em>values</em>:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="p">{</span><span class="s1">'a'</span><span class="p">:</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">0.2</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mf">0.2</span> <span class="o">+</span> <span class="mf">0.4</span><span class="p">}</span> <span class="o">==</span> <span class="n">approx</span><span class="p">({</span><span class="s1">'a'</span><span class="p">:</span> <span class="mf">0.3</span><span class="p">,</span> <span class="s1">'b'</span><span class="p">:</span> <span class="mf">0.6</span><span class="p">})</span>
<span class="go">True</span>
</pre></div>
</div>
<p>And <code class="docutils literal"><span class="pre">numpy</span></code> arrays:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="gp">>>> </span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.1</span><span class="p">,</span> <span class="mf">0.2</span><span class="p">])</span> <span class="o">+</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.2</span><span class="p">,</span> <span class="mf">0.4</span><span class="p">])</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">([</span><span class="mf">0.3</span><span class="p">,</span> <span class="mf">0.6</span><span class="p">]))</span>
<span class="go">True</span>
</pre></div>
</div>
<p>By default, <code class="docutils literal"><span class="pre">approx</span></code> considers numbers within a relative tolerance of
<code class="docutils literal"><span class="pre">1e-6</span></code> (i.e. one part in a million) of its expected value to be equal.
This treatment would lead to surprising results if the expected value was
<code class="docutils literal"><span class="pre">0.0</span></code>, because nothing but <code class="docutils literal"><span class="pre">0.0</span></code> itself is relatively close to <code class="docutils literal"><span class="pre">0.0</span></code>.
To handle this case less surprisingly, <code class="docutils literal"><span class="pre">approx</span></code> also considers numbers
within an absolute tolerance of <code class="docutils literal"><span class="pre">1e-12</span></code> of its expected value to be
equal. Infinity and NaN are special cases. Infinity is only considered
equal to itself, regardless of the relative tolerance. NaN is not
considered equal to anything by default, but you can make it be equal to
itself by setting the <code class="docutils literal"><span class="pre">nan_ok</span></code> argument to True. (This is meant to
facilitate comparing arrays that use NaN to mean "no data".)</p>
<p>Both the relative and absolute tolerances can be changed by passing
arguments to the <code class="docutils literal"><span class="pre">approx</span></code> constructor:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mf">1.0001</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="mf">1.0001</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">rel</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="mf">1.0001</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">abs</span><span class="o">=</span><span class="mf">1e-3</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you specify <code class="docutils literal"><span class="pre">abs</span></code> but not <code class="docutils literal"><span class="pre">rel</span></code>, the comparison will not consider
the relative tolerance at all. In other words, two numbers that are within
the default relative tolerance of <code class="docutils literal"><span class="pre">1e-6</span></code> will still be considered unequal
if they exceed the specified absolute tolerance. If you specify both
<code class="docutils literal"><span class="pre">abs</span></code> and <code class="docutils literal"><span class="pre">rel</span></code>, the numbers will be considered equal if either
tolerance is met:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="mf">1e-8</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="mf">1e-8</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="nb">abs</span><span class="o">=</span><span class="mf">1e-12</span><span class="p">)</span>
<span class="go">False</span>
<span class="gp">>>> </span><span class="mi">1</span> <span class="o">+</span> <span class="mf">1e-8</span> <span class="o">==</span> <span class="n">approx</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">rel</span><span class="o">=</span><span class="mf">1e-6</span><span class="p">,</span> <span class="nb">abs</span><span class="o">=</span><span class="mf">1e-12</span><span class="p">)</span>
<span class="go">True</span>
</pre></div>
</div>
<p>If you're thinking about using <code class="docutils literal"><span class="pre">approx</span></code>, then you might want to know how
it compares to other good ways of comparing floating-point numbers. All of
these algorithms are based on relative and absolute tolerances and should
agree for the most part, but they do have meaningful differences:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">math.isclose(a,</span> <span class="pre">b,</span> <span class="pre">rel_tol=1e-9,</span> <span class="pre">abs_tol=0.0)</span></code>: True if the relative
tolerance is met w.r.t. either <code class="docutils literal"><span class="pre">a</span></code> or <code class="docutils literal"><span class="pre">b</span></code> or if the absolute
tolerance is met. Because the relative tolerance is calculated w.r.t.
both <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code>, this test is symmetric (i.e. neither <code class="docutils literal"><span class="pre">a</span></code> nor
<code class="docutils literal"><span class="pre">b</span></code> is a "reference value"). You have to specify an absolute tolerance
if you want to compare to <code class="docutils literal"><span class="pre">0.0</span></code> because there is no tolerance by
default. Only available in python>=3.5. <a class="reference external" href="https://docs.python.org/3/library/math.html#math.isclose">More information...</a></li>
<li><code class="docutils literal"><span class="pre">numpy.isclose(a,</span> <span class="pre">b,</span> <span class="pre">rtol=1e-5,</span> <span class="pre">atol=1e-8)</span></code>: True if the difference
between <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code> is less that the sum of the relative tolerance
w.r.t. <code class="docutils literal"><span class="pre">b</span></code> and the absolute tolerance. Because the relative tolerance
is only calculated w.r.t. <code class="docutils literal"><span class="pre">b</span></code>, this test is asymmetric and you can
think of <code class="docutils literal"><span class="pre">b</span></code> as the reference value. Support for comparing sequences
is provided by <code class="docutils literal"><span class="pre">numpy.allclose</span></code>. <a class="reference external" href="http://docs.scipy.org/doc/numpy-1.10.0/reference/generated/numpy.isclose.html">More information...</a></li>
<li><code class="docutils literal"><span class="pre">unittest.TestCase.assertAlmostEqual(a,</span> <span class="pre">b)</span></code>: True if <code class="docutils literal"><span class="pre">a</span></code> and <code class="docutils literal"><span class="pre">b</span></code>
are within an absolute tolerance of <code class="docutils literal"><span class="pre">1e-7</span></code>. No relative tolerance is
considered and the absolute tolerance cannot be changed, so this function
is not appropriate for very large or very small numbers. Also, it's only
available in subclasses of <code class="docutils literal"><span class="pre">unittest.TestCase</span></code> and it's ugly because it
doesn't follow PEP8. <a class="reference external" href="https://docs.python.org/3/library/unittest.html#unittest.TestCase.assertAlmostEqual">More information...</a></li>
<li><code class="docutils literal"><span class="pre">a</span> <span class="pre">==</span> <span class="pre">pytest.approx(b,</span> <span class="pre">rel=1e-6,</span> <span class="pre">abs=1e-12)</span></code>: True if the relative
tolerance is met w.r.t. <code class="docutils literal"><span class="pre">b</span></code> or if the absolute tolerance is met.
Because the relative tolerance is only calculated w.r.t. <code class="docutils literal"><span class="pre">b</span></code>, this test
is asymmetric and you can think of <code class="docutils literal"><span class="pre">b</span></code> as the reference value. In the
special case that you explicitly specify an absolute tolerance but not a
relative tolerance, only the absolute tolerance is considered.</li>
</ul>
<div class="admonition warning">
<p class="first admonition-title">Warning</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2.</span></p>
</div>
<p>In order to avoid inconsistent behavior, <code class="docutils literal"><span class="pre">TypeError</span></code> is
raised for <code class="docutils literal"><span class="pre">></span></code>, <code class="docutils literal"><span class="pre">>=</span></code>, <code class="docutils literal"><span class="pre"><</span></code> and <code class="docutils literal"><span class="pre"><=</span></code> comparisons.
The example below illustrates the problem:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">assert</span> <span class="n">approx</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span> <span class="o">></span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">1e-10</span> <span class="c1"># calls approx(0.1).__gt__(0.1 + 1e-10)</span>
<span class="k">assert</span> <span class="mf">0.1</span> <span class="o">+</span> <span class="mf">1e-10</span> <span class="o">></span> <span class="n">approx</span><span class="p">(</span><span class="mf">0.1</span><span class="p">)</span> <span class="c1"># calls approx(0.1).__lt__(0.1 + 1e-10)</span>
</pre></div>
</div>
<p class="last">In the second example one expects <code class="docutils literal"><span class="pre">approx(0.1).__le__(0.1</span> <span class="pre">+</span> <span class="pre">1e-10)</span></code>
to be called. But instead, <code class="docutils literal"><span class="pre">approx(0.1).__lt__(0.1</span> <span class="pre">+</span> <span class="pre">1e-10)</span></code> is used to
comparison. This is because the call hierarchy of rich comparisons
follows a fixed behavior. <a class="reference external" href="https://docs.python.org/3/reference/datamodel.html#object.__ge__">More information...</a></p>
</div>
</dd></dl>
</div>
<div class="section" id="raising-a-specific-test-outcome">
<h2>Raising a specific test outcome<a class="headerlink" href="#raising-a-specific-test-outcome" title="Permalink to this headline">¶</a></h2>
<p>You can use the following functions in your test, fixture or setup
functions to force a certain test outcome. Note that most often
you can rather use declarative marks, see <a class="reference internal" href="skipping.html#skipping"><span class="std std-ref">Skip and xfail: dealing with tests that cannot succeed</span></a>.</p>
<dl class="function">
<dt id="_pytest.outcomes.fail">
<code class="descname">fail</code><span class="sig-paren">(</span><em>msg=''</em>, <em>pytrace=True</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/outcomes.html#fail"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.outcomes.fail" title="Permalink to this definition">¶</a></dt>
<dd><p>explicitly fail an currently-executing test with the given Message.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>pytrace</strong> -- if false the msg represents the full failure information
and no python traceback will be reported.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="_pytest.outcomes.skip">
<code class="descname">skip</code><span class="sig-paren">(</span><em>msg=''</em>, <em>**kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/outcomes.html#skip"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.outcomes.skip" title="Permalink to this definition">¶</a></dt>
<dd><p>skip an executing test with the given message. Note: it's usually
better to use the pytest.mark.skipif marker to declare a test to be
skipped under certain conditions like mismatching platforms or
dependencies. See the pytest_skipping plugin for details.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>allow_module_level</strong> (<em>bool</em>) -- allows this function to be called at
module level, skipping the rest of the module. Default to False.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="function">
<dt id="_pytest.outcomes.importorskip">
<code class="descname">importorskip</code><span class="sig-paren">(</span><em>modname</em>, <em>minversion=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/outcomes.html#importorskip"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.outcomes.importorskip" title="Permalink to this definition">¶</a></dt>
<dd><p>return imported module if it has at least "minversion" as its
__version__ attribute. If no minversion is specified the a skip
is only triggered if the module can not be imported.</p>
</dd></dl>
<dl class="function">
<dt id="_pytest.outcomes.xfail">
<code class="descname">xfail</code><span class="sig-paren">(</span><em>reason=''</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/outcomes.html#xfail"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.outcomes.xfail" title="Permalink to this definition">¶</a></dt>
<dd><p>xfail an executing test or setup functions with the given reason.</p>
</dd></dl>
<dl class="function">
<dt id="_pytest.outcomes.exit">
<code class="descname">exit</code><span class="sig-paren">(</span><em>msg</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/outcomes.html#exit"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.outcomes.exit" title="Permalink to this definition">¶</a></dt>
<dd><p>exit testing process as if KeyboardInterrupt was triggered.</p>
</dd></dl>
</div>
<div class="section" id="fixtures-and-requests">
<h2>Fixtures and requests<a class="headerlink" href="#fixtures-and-requests" title="Permalink to this headline">¶</a></h2>
<p>To mark a fixture function:</p>
<dl class="function">
<dt id="_pytest.fixtures.fixture">
<code class="descname">fixture</code><span class="sig-paren">(</span><em>scope='function'</em>, <em>params=None</em>, <em>autouse=False</em>, <em>ids=None</em>, <em>name=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#fixture"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.fixture" title="Permalink to this definition">¶</a></dt>
<dd><p>(return a) decorator to mark a fixture factory function.</p>
<p>This decorator can be used (with or without parameters) to define a
fixture function. The name of the fixture function can later be
referenced to cause its invocation ahead of running tests: test
modules or classes can use the pytest.mark.usefixtures(fixturename)
marker. Test functions can directly use fixture names as input
arguments in which case the fixture instance returned from the fixture
function will be injected.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>scope</strong> -- the scope for which this fixture is shared, one of
"function" (default), "class", "module" or "session".</li>
<li><strong>params</strong> -- an optional list of parameters which will cause multiple
invocations of the fixture function and all of the tests
using it.</li>
<li><strong>autouse</strong> -- if True, the fixture func is activated for all tests that
can see it. If False (the default) then an explicit
reference is needed to activate the fixture.</li>
<li><strong>ids</strong> -- list of string ids each corresponding to the params
so that they are part of the test id. If no ids are provided
they will be generated automatically from the params.</li>
<li><strong>name</strong> -- the name of the fixture. This defaults to the name of the
decorated function. If a fixture is used in the same module in
which it is defined, the function name of the fixture will be
shadowed by the function arg that requests the fixture; one way
to resolve this is to name the decorated function
<code class="docutils literal"><span class="pre">fixture_<fixturename></span></code> and then use
<code class="docutils literal"><span class="pre">@pytest.fixture(name='<fixturename>')</span></code>.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<p>Fixtures can optionally provide their values to test functions using a <code class="docutils literal"><span class="pre">yield</span></code> statement,
instead of <code class="docutils literal"><span class="pre">return</span></code>. In this case, the code block after the <code class="docutils literal"><span class="pre">yield</span></code> statement is executed
as teardown code regardless of the test outcome. A fixture function must yield exactly once.</p>
</dd></dl>
<p>Tutorial at <a class="reference internal" href="fixture.html#fixtures"><span class="std std-ref">pytest fixtures: explicit, modular, scalable</span></a>.</p>
<p>The <code class="docutils literal"><span class="pre">request</span></code> object that can be used from fixture functions.</p>
<dl class="class">
<dt id="_pytest.fixtures.FixtureRequest">
<em class="property">class </em><code class="descname">FixtureRequest</code><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest" title="Permalink to this definition">¶</a></dt>
<dd><p>A request for a fixture from a test or fixture function.</p>
<p>A request object gives access to the requesting test context
and has an optional <code class="docutils literal"><span class="pre">param</span></code> attribute in case
the fixture is parametrized indirectly.</p>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.fixturename">
<code class="descname">fixturename</code><em class="property"> = None</em><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.fixturename" title="Permalink to this definition">¶</a></dt>
<dd><p>fixture for which this request is being performed</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.scope">
<code class="descname">scope</code><em class="property"> = None</em><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.scope" title="Permalink to this definition">¶</a></dt>
<dd><p>Scope string, one of "function", "class", "module", "session"</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.node">
<code class="descname">node</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.node" title="Permalink to this definition">¶</a></dt>
<dd><p>underlying collection node (depends on current request scope)</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.config">
<code class="descname">config</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.config" title="Permalink to this definition">¶</a></dt>
<dd><p>the pytest config object associated with this request.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.function">
<code class="descname">function</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.function" title="Permalink to this definition">¶</a></dt>
<dd><p>test function object if the request has a per-function scope.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.cls">
<code class="descname">cls</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.cls" title="Permalink to this definition">¶</a></dt>
<dd><p>class (can be None) where the test function was collected.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.instance">
<code class="descname">instance</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.instance" title="Permalink to this definition">¶</a></dt>
<dd><p>instance (can be None) on which test function was collected.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.module">
<code class="descname">module</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.module" title="Permalink to this definition">¶</a></dt>
<dd><p>python module object where the test function was collected.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.fspath">
<code class="descname">fspath</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.fspath" title="Permalink to this definition">¶</a></dt>
<dd><p>the file system path of the test module which collected this test.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.keywords">
<code class="descname">keywords</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.keywords" title="Permalink to this definition">¶</a></dt>
<dd><p>keywords/markers dictionary for the underlying node.</p>
</dd></dl>
<dl class="attribute">
<dt id="_pytest.fixtures.FixtureRequest.session">
<code class="descname">session</code><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.session" title="Permalink to this definition">¶</a></dt>
<dd><p>pytest session object.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.addfinalizer">
<code class="descname">addfinalizer</code><span class="sig-paren">(</span><em>finalizer</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.addfinalizer"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.addfinalizer" title="Permalink to this definition">¶</a></dt>
<dd><p>add finalizer/teardown function to be called after the
last test within the requesting test context finished
execution.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.applymarker">
<code class="descname">applymarker</code><span class="sig-paren">(</span><em>marker</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.applymarker"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.applymarker" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply a marker to a single test function invocation.
This method is useful if you don't want to have a keyword/marker
on all function invocations.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>marker</strong> -- a <a class="reference internal" href="mark.html#_pytest.mark.MarkDecorator" title="_pytest.mark.MarkDecorator"><code class="xref py py-class docutils literal"><span class="pre">_pytest.mark.MarkDecorator</span></code></a> object
created by a call to <code class="docutils literal"><span class="pre">pytest.mark.NAME(...)</span></code>.</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.raiseerror">
<code class="descname">raiseerror</code><span class="sig-paren">(</span><em>msg</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.raiseerror"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.raiseerror" title="Permalink to this definition">¶</a></dt>
<dd><p>raise a FixtureLookupError with the given message.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.cached_setup">
<code class="descname">cached_setup</code><span class="sig-paren">(</span><em>setup</em>, <em>teardown=None</em>, <em>scope='module'</em>, <em>extrakey=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.cached_setup"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.cached_setup" title="Permalink to this definition">¶</a></dt>
<dd><p>(deprecated) Return a testing resource managed by <code class="docutils literal"><span class="pre">setup</span></code> &
<code class="docutils literal"><span class="pre">teardown</span></code> calls. <code class="docutils literal"><span class="pre">scope</span></code> and <code class="docutils literal"><span class="pre">extrakey</span></code> determine when the
<code class="docutils literal"><span class="pre">teardown</span></code> function will be called so that subsequent calls to
<code class="docutils literal"><span class="pre">setup</span></code> would recreate the resource. With pytest-2.3 you often
do not need <code class="docutils literal"><span class="pre">cached_setup()</span></code> as you can directly declare a scope
on a fixture function and register a finalizer through
<code class="docutils literal"><span class="pre">request.addfinalizer()</span></code>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>teardown</strong> -- function receiving a previously setup resource.</li>
<li><strong>setup</strong> -- a no-argument function creating a resource.</li>
<li><strong>scope</strong> -- a string value out of <code class="docutils literal"><span class="pre">function</span></code>, <code class="docutils literal"><span class="pre">class</span></code>, <code class="docutils literal"><span class="pre">module</span></code>
or <code class="docutils literal"><span class="pre">session</span></code> indicating the caching lifecycle of the resource.</li>
<li><strong>extrakey</strong> -- added to internal caching key of (funcargname, scope).</li>
</ul>
</td>
</tr>
</tbody>
</table>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.getfixturevalue">
<code class="descname">getfixturevalue</code><span class="sig-paren">(</span><em>argname</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.getfixturevalue"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.getfixturevalue" title="Permalink to this definition">¶</a></dt>
<dd><p>Dynamically run a named fixture function.</p>
<p>Declaring fixtures via function argument is recommended where possible.
But if you can only decide whether to use another fixture at test
setup time, you may use this function to retrieve it inside a fixture
or test function body.</p>
</dd></dl>
<dl class="method">
<dt id="_pytest.fixtures.FixtureRequest.getfuncargvalue">
<code class="descname">getfuncargvalue</code><span class="sig-paren">(</span><em>argname</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/_pytest/fixtures.html#FixtureRequest.getfuncargvalue"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#_pytest.fixtures.FixtureRequest.getfuncargvalue" title="Permalink to this definition">¶</a></dt>
<dd><p>Deprecated, use getfixturevalue.</p>
</dd></dl>
</dd></dl>
</div>
<div class="section" id="builtin-fixtures-function-arguments">
<span id="builtinfuncargs"></span><span id="builtinfixtures"></span><h2>Builtin fixtures/function arguments<a class="headerlink" href="#builtin-fixtures-function-arguments" title="Permalink to this headline">¶</a></h2>
<p>You can ask for available builtin or project-custom
<a class="reference internal" href="fixture.html#fixtures"><span class="std std-ref">fixtures</span></a> by typing:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span>$ pytest -q --fixtures
cache
Return a cache object that can persist state between testing sessions.
cache.get(key, default)
cache.set(key, value)
Keys must be a ``/`` separated value, where the first part is usually the
name of your plugin or application to avoid clashes with other cache users.
Values can be any object handled by the json stdlib module.
capsys
Enable capturing of writes to sys.stdout/sys.stderr and make
captured output available via ``capsys.readouterr()`` method calls
which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``text``
objects.
capsysbinary
Enable capturing of writes to sys.stdout/sys.stderr and make
captured output available via ``capsys.readouterr()`` method calls
which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``bytes``
objects.
capfd
Enable capturing of writes to file descriptors 1 and 2 and make
captured output available via ``capfd.readouterr()`` method calls
which return a ``(out, err)`` tuple. ``out`` and ``err`` will be ``text``
objects.
capfdbinary
Enable capturing of write to file descriptors 1 and 2 and make
captured output available via ``capfdbinary.readouterr`` method calls
which return a ``(out, err)`` tuple. ``out`` and ``err`` will be
``bytes`` objects.
doctest_namespace
Inject names into the doctest namespace.
pytestconfig
the pytest config object with access to command line opts.
record_xml_property
Add extra xml properties to the tag for the calling test.
The fixture is callable with ``(name, value)``, with value being automatically
xml-encoded.
caplog
Access and control log capturing.
Captured logs are available through the following methods::
* caplog.text() -> string containing formatted log output
* caplog.records() -> list of logging.LogRecord instances
* caplog.record_tuples() -> list of (logger_name, level, message) tuples
monkeypatch
The returned ``monkeypatch`` fixture provides these
helper methods to modify objects, dictionaries or os.environ::
monkeypatch.setattr(obj, name, value, raising=True)
monkeypatch.delattr(obj, name, raising=True)
monkeypatch.setitem(mapping, name, value)
monkeypatch.delitem(obj, name, raising=True)
monkeypatch.setenv(name, value, prepend=False)
monkeypatch.delenv(name, value, raising=True)
monkeypatch.syspath_prepend(path)
monkeypatch.chdir(path)
All modifications will be undone after the requesting
test function or fixture has finished. The ``raising``
parameter determines if a KeyError or AttributeError
will be raised if the set/deletion operation has no target.
recwarn
Return a WarningsRecorder instance that provides these methods:
* ``pop(category=None)``: return last warning matching the category.
* ``clear()``: clear list of warnings
See http://docs.python.org/library/warnings.html for information
on warning categories.
tmpdir_factory
Return a TempdirFactory instance for the test session.
tmpdir
Return a temporary directory path object
which is unique to each test function invocation,
created as a sub directory of the base temporary
directory. The returned object is a `py.path.local`_
path object.
no tests ran in 0.12 seconds
</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="#">Pytest API and builtin fixtures</a><ul>
<li><a class="reference internal" href="#invoking-pytest-interactively">Invoking pytest interactively</a></li>
<li><a class="reference internal" href="#helpers-for-assertions-about-exceptions-warnings">Helpers for assertions about Exceptions/Warnings</a></li>
<li><a class="reference internal" href="#comparing-floating-point-numbers">Comparing floating point numbers</a></li>
<li><a class="reference internal" href="#raising-a-specific-test-outcome">Raising a specific test outcome</a></li>
<li><a class="reference internal" href="#fixtures-and-requests">Fixtures and requests</a></li>
<li><a class="reference internal" href="#builtin-fixtures-function-arguments">Builtin fixtures/function arguments</a></li>
</ul>
</li>
</ul>
<h3>Related Topics</h3>
<ul>
<li><a href="contents.html">Documentation overview</a><ul>
<li>Previous: <a href="assert.html" title="previous chapter">The writing and reporting of assertions in tests</a></li>
<li>Next: <a href="fixture.html" title="next chapter">pytest fixtures: explicit, modular, scalable</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>
|