/usr/share/doc/python-hypothesis/html/details.html is in python-hypothesis-doc 3.44.1-2ubuntu1.
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 | <!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Details and advanced features — Hypothesis 3.44.1 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="index" title="Index"
href="genindex.html"/>
<link rel="search" title="Search" href="search.html"/>
<link rel="top" title="Hypothesis 3.44.1 documentation" href="index.html"/>
<link rel="next" title="Settings" href="settings.html"/>
<link rel="prev" title="Quick start guide" href="quickstart.html"/>
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav" role="document">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html" class="icon icon-home"> Hypothesis
</a>
<div class="version">
3.44.1
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="quickstart.html">Quick start guide</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Details and advanced features</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#additional-test-output">Additional test output</a></li>
<li class="toctree-l2"><a class="reference internal" href="#test-statistics">Test Statistics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#making-assumptions">Making assumptions</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#how-good-is-assume">How good is assume?</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#defining-strategies">Defining strategies</a></li>
<li class="toctree-l2"><a class="reference internal" href="#the-gory-details-of-given-parameters">The gory details of given parameters</a></li>
<li class="toctree-l2"><a class="reference internal" href="#custom-function-execution">Custom function execution</a></li>
<li class="toctree-l2"><a class="reference internal" href="#using-hypothesis-to-find-values">Using Hypothesis to find values</a></li>
<li class="toctree-l2"><a class="reference internal" href="#inferred-strategies">Inferred Strategies</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#limitations">Limitations</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">Settings</a></li>
<li class="toctree-l1"><a class="reference internal" href="data.html">What you can generate and how</a></li>
<li class="toctree-l1"><a class="reference internal" href="extras.html">Additional packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="django.html">Hypothesis for Django users</a></li>
<li class="toctree-l1"><a class="reference internal" href="numpy.html">Hypothesis for the Scientific Stack</a></li>
<li class="toctree-l1"><a class="reference internal" href="healthchecks.html">Health checks</a></li>
<li class="toctree-l1"><a class="reference internal" href="database.html">The Hypothesis Example Database</a></li>
<li class="toctree-l1"><a class="reference internal" href="stateful.html">Stateful testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="supported.html">Compatibility</a></li>
<li class="toctree-l1"><a class="reference internal" href="examples.html">Some more examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="community.html">Community</a></li>
<li class="toctree-l1"><a class="reference internal" href="manifesto.html">The Purpose of Hypothesis</a></li>
<li class="toctree-l1"><a class="reference internal" href="endorsements.html">Testimonials</a></li>
<li class="toctree-l1"><a class="reference internal" href="usage.html">Open Source Projects using Hypothesis</a></li>
<li class="toctree-l1"><a class="reference internal" href="strategies.html">Projects extending Hypothesis</a></li>
<li class="toctree-l1"><a class="reference internal" href="changes.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="development.html">Ongoing Hypothesis Development</a></li>
<li class="toctree-l1"><a class="reference internal" href="support.html">Help and Support</a></li>
<li class="toctree-l1"><a class="reference internal" href="packaging.html">Packaging Guidelines</a></li>
<li class="toctree-l1"><a class="reference internal" href="reproducing.html">Reproducing Failures</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" role="navigation" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Hypothesis</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Details and advanced features</li>
<li class="wy-breadcrumbs-aside">
<a href="_sources/details.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="details-and-advanced-features">
<h1>Details and advanced features<a class="headerlink" href="#details-and-advanced-features" title="Permalink to this headline">¶</a></h1>
<p>This is an account of slightly less common Hypothesis features that you don’t need
to get started but will nevertheless make your life easier.</p>
<div class="section" id="additional-test-output">
<h2>Additional test output<a class="headerlink" href="#additional-test-output" title="Permalink to this headline">¶</a></h2>
<p>Normally the output of a failing test will look something like:</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="n">Falsifying</span> <span class="n">example</span><span class="p">:</span> <span class="n">test_a_thing</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">y</span><span class="o">=</span><span class="s2">"foo"</span><span class="p">)</span>
</pre></div>
</div>
<p>With the <code class="docutils literal"><span class="pre">repr</span></code> of each keyword argument being printed.</p>
<p>Sometimes this isn’t enough, either because you have values with a <code class="docutils literal"><span class="pre">repr</span></code> that
isn’t very descriptive or because you need to see the output of some
intermediate steps of your test. That’s where the <code class="docutils literal"><span class="pre">note</span></code> function comes in:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hypothesis</span> <span class="k">import</span> <span class="n">given</span><span class="p">,</span> <span class="n">note</span><span class="p">,</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="gp">>>> </span><span class="nd">@given</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">lists</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">integers</span><span class="p">()),</span> <span class="n">st</span><span class="o">.</span><span class="n">randoms</span><span class="p">())</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">test_shuffle_is_noop</span><span class="p">(</span><span class="n">ls</span><span class="p">,</span> <span class="n">r</span><span class="p">):</span>
<span class="gp">... </span> <span class="n">ls2</span> <span class="o">=</span> <span class="nb">list</span><span class="p">(</span><span class="n">ls</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">r</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">ls2</span><span class="p">)</span>
<span class="gp">... </span> <span class="n">note</span><span class="p">(</span><span class="s2">"Shuffle: </span><span class="si">%r</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">ls2</span><span class="p">))</span>
<span class="gp">... </span> <span class="k">assert</span> <span class="n">ls</span> <span class="o">==</span> <span class="n">ls2</span>
<span class="gp">...</span>
<span class="gp">>>> </span><span class="k">try</span><span class="p">:</span>
<span class="gp">... </span> <span class="n">test_shuffle_is_noop</span><span class="p">()</span>
<span class="gp">... </span><span class="k">except</span> <span class="ne">AssertionError</span><span class="p">:</span>
<span class="gp">... </span> <span class="nb">print</span><span class="p">(</span><span class="s1">'ls != ls2'</span><span class="p">)</span>
<span class="go">Falsifying example: test_shuffle_is_noop(ls=[0, 0, 1], r=RandomWithSeed(0))</span>
<span class="go">Shuffle: [0, 1, 0]</span>
<span class="go">ls != ls2</span>
</pre></div>
</div>
<p>The note is printed in the final run of the test in order to include any
additional information you might need in your test.</p>
</div>
<div class="section" id="test-statistics">
<span id="statistics"></span><h2>Test Statistics<a class="headerlink" href="#test-statistics" title="Permalink to this headline">¶</a></h2>
<p>If you are using py.test you can see a number of statistics about the executed tests
by passing the command line argument <code class="docutils literal"><span class="pre">--hypothesis-show-statistics</span></code>. This will include
some general statistics about the test:</p>
<p>For example if you ran the following with <code class="docutils literal"><span class="pre">--hypothesis-show-statistics</span></code>:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">hypothesis</span> <span class="kn">import</span> <span class="n">given</span><span class="p">,</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_integers</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>You would see:</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>test_integers:
- 100 passing examples, 0 failing examples, 0 invalid examples
- Typical runtimes: ~ 1ms
- Fraction of time spent in data generation: ~ 12%
- Stopped because settings.max_examples=100
</pre></div>
</div>
<p>The final “Stopped because” line is particularly important to note: It tells you the
setting value that determined when the test should stop trying new examples. This
can be useful for understanding the behaviour of your tests. Ideally you’d always want
this to be <code class="docutils literal"><span class="pre">max_examples</span></code>.</p>
<p>In some cases (such as filtered and recursive strategies) you will see events mentioned
which describe some aspect of the data generation:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">hypothesis</span> <span class="kn">import</span> <span class="n">given</span><span class="p">,</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">integers</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">test_even_integers</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>You would see something like:</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>test_even_integers:
- 100 passing examples, 0 failing examples, 36 invalid examples
- Typical runtimes: 0-1 ms
- Fraction of time spent in data generation: ~ 16%
- Stopped because settings.max_examples=100
- Events:
* 80.88%, Retried draw from integers().filter(lambda x: <unknown>) to satisfy filter
* 26.47%, Aborted test because unable to satisfy integers().filter(lambda x: <unknown>)
</pre></div>
</div>
<p>You can also mark custom events in a test using the <code class="docutils literal"><span class="pre">event</span></code> function:</p>
<dl class="function">
<dt id="hypothesis.event">
<code class="descclassname">hypothesis.</code><code class="descname">event</code><span class="sig-paren">(</span><em>value</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/control.html#event"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.event" title="Permalink to this definition">¶</a></dt>
<dd><p>Record an event that occurred this test. Statistics on number of test
runs with each event will be reported at the end if you run Hypothesis in
statistics reporting mode.</p>
<p>Events should be strings or convertible to them.</p>
</dd></dl>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">hypothesis</span> <span class="k">import</span> <span class="n">given</span><span class="p">,</span> <span class="n">event</span><span class="p">,</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">st</span><span class="o">.</span><span class="n">integers</span><span class="p">()</span><span class="o">.</span><span class="n">filter</span><span class="p">(</span><span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">x</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span><span class="p">))</span>
<span class="k">def</span> <span class="nf">test_even_integers</span><span class="p">(</span><span class="n">i</span><span class="p">):</span>
<span class="n">event</span><span class="p">(</span><span class="s2">"i mod 3 = </span><span class="si">%d</span><span class="s2">"</span> <span class="o">%</span> <span class="p">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">3</span><span class="p">,))</span>
</pre></div>
</div>
<p>You will then see output like:</p>
<div class="highlight-none"><div class="highlight"><pre><span></span>test_even_integers:
- 100 passing examples, 0 failing examples, 38 invalid examples
- Typical runtimes: 0-1 ms
- Fraction of time spent in data generation: ~ 16%
- Stopped because settings.max_examples=100
- Events:
* 80.43%, Retried draw from integers().filter(lambda x: <unknown>) to satisfy filter
* 31.88%, i mod 3 = 0
* 27.54%, Aborted test because unable to satisfy integers().filter(lambda x: <unknown>)
* 21.74%, i mod 3 = 1
* 18.84%, i mod 3 = 2
</pre></div>
</div>
<p>Arguments to <code class="docutils literal"><span class="pre">event</span></code> can be any hashable type, but two events will be considered the same
if they are the same when converted to a string with <code class="docutils literal"><span class="pre">str</span></code>.</p>
</div>
<div class="section" id="making-assumptions">
<h2>Making assumptions<a class="headerlink" href="#making-assumptions" title="Permalink to this headline">¶</a></h2>
<p>Sometimes Hypothesis doesn’t give you exactly the right sort of data you want - it’s
mostly of the right shape, but some examples won’t work and you don’t want to care about
them. You <em>can</em> just ignore these by aborting the test early, but this runs the risk of
accidentally testing a lot less than you think you are. Also it would be nice to spend
less time on bad examples - if you’re running 200 examples per test (the default) and
it turns out 150 of those examples don’t match your needs, that’s a lot of wasted time.</p>
<dl class="function">
<dt id="hypothesis.assume">
<code class="descclassname">hypothesis.</code><code class="descname">assume</code><span class="sig-paren">(</span><em>condition</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/control.html#assume"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.assume" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal"><span class="pre">assume()</span></code> is like an <span class="xref std std-ref">assert</span> that marks the
example as bad, rather than failing the test.</p>
<p>This allows you to specify properties that you <em>assume</em> will be
true, and let Hypothesis try to avoid similar examples in future.</p>
</dd></dl>
<p>For example suppose you had the following test:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">floats</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_negation_is_self_inverse</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">x</span> <span class="o">==</span> <span class="o">-</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>Running this gives us:</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="n">Falsifying</span> <span class="n">example</span><span class="p">:</span> <span class="n">test_negation_is_self_inverse</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="nb">float</span><span class="p">(</span><span class="s1">'nan'</span><span class="p">))</span>
<span class="ne">AssertionError</span>
</pre></div>
</div>
<p>This is annoying. We know about NaN and don’t really care about it, but as soon as Hypothesis
finds a NaN example it will get distracted by that and tell us about it. Also the test will
fail and we want it to pass.</p>
<p>So lets block off this particular example:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">isnan</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">floats</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_negation_is_self_inverse_for_non_nan</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">assume</span><span class="p">(</span><span class="ow">not</span> <span class="n">isnan</span><span class="p">(</span><span class="n">x</span><span class="p">))</span>
<span class="k">assert</span> <span class="n">x</span> <span class="o">==</span> <span class="o">-</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>And this passes without a problem.</p>
<p>In order to avoid the easy trap where you assume a lot more than you intended, Hypothesis
will fail a test when it can’t find enough examples passing the assumption.</p>
<p>If we’d written:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">floats</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_negation_is_self_inverse_for_non_nan</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="n">assume</span><span class="p">(</span><span class="kc">False</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">x</span> <span class="o">==</span> <span class="o">-</span><span class="p">(</span><span class="o">-</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<p>Then on running we’d have got the exception:</p>
<div class="code highlight-default"><div class="highlight"><pre><span></span><span class="n">Unsatisfiable</span><span class="p">:</span> <span class="n">Unable</span> <span class="n">to</span> <span class="n">satisfy</span> <span class="n">assumptions</span> <span class="n">of</span> <span class="n">hypothesis</span> <span class="n">test_negation_is_self_inverse_for_non_nan</span><span class="o">.</span> <span class="n">Only</span> <span class="mi">0</span> <span class="n">examples</span> <span class="n">considered</span> <span class="n">satisfied</span> <span class="n">assumptions</span>
</pre></div>
</div>
<div class="section" id="how-good-is-assume">
<h3>How good is assume?<a class="headerlink" href="#how-good-is-assume" title="Permalink to this headline">¶</a></h3>
<p>Hypothesis has an adaptive exploration strategy to try to avoid things which falsify
assumptions, which should generally result in it still being able to find examples in
hard to find situations.</p>
<p>Suppose we had the following:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">lists</span><span class="p">(</span><span class="n">integers</span><span class="p">()))</span>
<span class="k">def</span> <span class="nf">test_sum_is_positive</span><span class="p">(</span><span class="n">xs</span><span class="p">):</span>
<span class="k">assert</span> <span class="nb">sum</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span>
</pre></div>
</div>
<p>Unsurprisingly this fails and gives the falsifying example <code class="docutils literal"><span class="pre">[]</span></code>.</p>
<p>Adding <code class="docutils literal"><span class="pre">assume(xs)</span></code> to this removes the trivial empty example and gives us <code class="docutils literal"><span class="pre">[0]</span></code>.</p>
<p>Adding <code class="docutils literal"><span class="pre">assume(all(x</span> <span class="pre">></span> <span class="pre">0</span> <span class="pre">for</span> <span class="pre">x</span> <span class="pre">in</span> <span class="pre">xs))</span></code> and it passes: the sum of a list of
positive integers is positive.</p>
<p>The reason that this should be surprising is not that it doesn’t find a
counter-example, but that it finds enough examples at all.</p>
<p>In order to make sure something interesting is happening, suppose we wanted to
try this for long lists. e.g. suppose we added an <code class="docutils literal"><span class="pre">assume(len(xs)</span> <span class="pre">></span> <span class="pre">10)</span></code> to it.
This should basically never find an example: a naive strategy would find fewer
than one in a thousand examples, because if each element of the list is
negative with probability one-half, you’d have to have ten of these go the right
way by chance. In the default configuration Hypothesis gives up long before
it’s tried 1000 examples (by default it tries 200).</p>
<p>Here’s what happens if we try to run this:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">lists</span><span class="p">(</span><span class="n">integers</span><span class="p">()))</span>
<span class="k">def</span> <span class="nf">test_sum_is_positive</span><span class="p">(</span><span class="n">xs</span><span class="p">):</span>
<span class="n">assume</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span> <span class="o">></span> <span class="mi">10</span><span class="p">)</span>
<span class="n">assume</span><span class="p">(</span><span class="nb">all</span><span class="p">(</span><span class="n">x</span> <span class="o">></span> <span class="mi">0</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">xs</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">sum</span><span class="p">(</span><span class="n">xs</span><span class="p">)</span> <span class="o">></span> <span class="mi">0</span>
<span class="n">In</span><span class="p">:</span> <span class="n">test_sum_is_positive</span><span class="p">()</span>
<span class="p">[</span><span class="mi">17</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">7</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">11</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="mi">9</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">47</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">31</span><span class="p">,</span> <span class="mi">1</span><span class="p">]</span>
<span class="p">[</span><span class="mi">6</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">29</span><span class="p">,</span> <span class="mi">30</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">34</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">50</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">16</span><span class="p">]</span>
<span class="p">[</span><span class="mi">25</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">19</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">22</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">17</span><span class="p">,</span> <span class="mi">13</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">16</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mf">2.</span><span class="o">..</span>
<span class="p">[</span><span class="mi">17</span><span class="p">,</span> <span class="mi">65</span><span class="p">,</span> <span class="mi">78</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">29</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">79</span><span class="p">,</span> <span class="mi">28</span><span class="p">,</span> <span class="mi">18</span><span class="p">,</span> <span class="mi">39</span><span class="p">]</span>
<span class="p">[</span><span class="mi">13</span><span class="p">,</span> <span class="mi">26</span><span class="p">,</span> <span class="mi">8</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="mi">76</span><span class="p">,</span> <span class="mi">6</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">20</span><span class="p">,</span> <span class="mi">27</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">32</span><span class="p">,</span> <span class="mi">14</span><span class="p">,</span> <span class="mi">42</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">24</span><span class="p">,</span> <span class="mi">33</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">15</span><span class="p">,</span> <span class="o">...</span>
<span class="p">[</span><span class="mi">2</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="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">12</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="mi">11</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">16</span><span class="p">]</span>
</pre></div>
</div>
<p>As you can see, Hypothesis doesn’t find <em>many</em> examples here, but it finds some - enough to
keep it happy.</p>
<p>In general if you <em>can</em> shape your strategies better to your tests you should - for example
<a class="reference internal" href="data.html#hypothesis.strategies.integers" title="hypothesis.strategies.integers"><code class="xref py py-func docutils literal"><span class="pre">integers(1,</span> <span class="pre">1000)</span></code></a> is a lot better than
<code class="docutils literal"><span class="pre">assume(1</span> <span class="pre"><=</span> <span class="pre">x</span> <span class="pre"><=</span> <span class="pre">1000)</span></code>, but <code class="docutils literal"><span class="pre">assume</span></code> will take you a long way if you can’t.</p>
</div>
</div>
<div class="section" id="defining-strategies">
<h2>Defining strategies<a class="headerlink" href="#defining-strategies" title="Permalink to this headline">¶</a></h2>
<p>The type of object that is used to explore the examples given to your test
function is called a <code class="xref py py-class docutils literal"><span class="pre">SearchStrategy</span></code>.
These are created using the functions
exposed in the <a class="reference internal" href="data.html#module-hypothesis.strategies" title="hypothesis.strategies"><code class="xref py py-mod docutils literal"><span class="pre">hypothesis.strategies</span></code></a> module.</p>
<p>Many of these strategies expose a variety of arguments you can use to customize
generation. For example for integers you can specify <code class="docutils literal"><span class="pre">min</span></code> and <code class="docutils literal"><span class="pre">max</span></code> values of
integers you want.
If you want to see exactly what a strategy produces you can ask for an example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">integers</span><span class="p">(</span><span class="n">min_value</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">max_value</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span><span class="o">.</span><span class="n">example</span><span class="p">()</span>
<span class="go">9</span>
</pre></div>
</div>
<p>Many strategies are built out of other strategies. For example, if you want
to define a tuple you need to say what goes in each element:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hypothesis.strategies</span> <span class="k">import</span> <span class="n">tuples</span>
<span class="gp">>>> </span><span class="n">tuples</span><span class="p">(</span><span class="n">integers</span><span class="p">(),</span> <span class="n">integers</span><span class="p">())</span><span class="o">.</span><span class="n">example</span><span class="p">()</span>
<span class="go">(-85296636193678268231691518597782489127, 68871684356256783618296489618877951982)</span>
</pre></div>
</div>
<p>Further details are <a class="reference internal" href="data.html"><span class="doc">available in a separate document</span></a>.</p>
</div>
<div class="section" id="the-gory-details-of-given-parameters">
<h2>The gory details of given parameters<a class="headerlink" href="#the-gory-details-of-given-parameters" title="Permalink to this headline">¶</a></h2>
<dl class="function">
<dt id="hypothesis.given">
<code class="descclassname">hypothesis.</code><code class="descname">given</code><span class="sig-paren">(</span><em>*given_arguments</em>, <em>**given_kwargs</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/core.html#given"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.given" title="Permalink to this definition">¶</a></dt>
<dd><p>A decorator for turning a test function that accepts arguments into a
randomized test.</p>
<p>This is the main entry point to Hypothesis.</p>
</dd></dl>
<p>The <a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">@given</span></code></a> decorator may be used
to specify which arguments of a function should
be parametrized over. You can use either positional or keyword arguments or a mixture
of the two.</p>
<p>For example all of the following are valid uses:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">(),</span> <span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">a</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">b</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">y</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">c</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">d</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">integers</span><span class="p">(),</span> <span class="n">y</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">e</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">x</span><span class="o">=</span><span class="n">integers</span><span class="p">(),</span> <span class="n">y</span><span class="o">=</span><span class="n">integers</span><span class="p">())</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="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">class</span> <span class="nc">SomeTest</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_a_thing</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>The following are not:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">(),</span> <span class="n">integers</span><span class="p">(),</span> <span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">g</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">h</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">(),</span> <span class="n">x</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">i</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
<span class="nd">@given</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">j</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
<span class="k">pass</span>
</pre></div>
</div>
<p>The rules for determining what are valid uses of <code class="docutils literal"><span class="pre">given</span></code> are as follows:</p>
<ol class="arabic simple">
<li>You may pass any keyword argument to <code class="docutils literal"><span class="pre">given</span></code>.</li>
<li>Positional arguments to <code class="docutils literal"><span class="pre">given</span></code> are equivalent to the rightmost named
arguments for the test function.</li>
<li>Positional arguments may not be used if the underlying test function has
varargs, arbitrary keywords, or keyword-only arguments.</li>
<li>Functions tested with <code class="docutils literal"><span class="pre">given</span></code> may not have any defaults.</li>
</ol>
<p>The reason for the “rightmost named arguments” behaviour is so that
using <a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">@given</span></code></a> with instance methods works: <code class="docutils literal"><span class="pre">self</span></code>
will be passed to the function as normal and not be parametrized over.</p>
<p>The function returned by given has all the same arguments as the original
test, minus those that are filled in by <code class="docutils literal"><span class="pre">given</span></code>.</p>
</div>
<div class="section" id="custom-function-execution">
<h2>Custom function execution<a class="headerlink" href="#custom-function-execution" title="Permalink to this headline">¶</a></h2>
<p>Hypothesis provides you with a hook that lets you control how it runs
examples.</p>
<p>This lets you do things like set up and tear down around each example, run
examples in a subprocess, transform coroutine tests into normal tests, etc.</p>
<p>The way this works is by introducing the concept of an executor. An executor
is essentially a function that takes a block of code and run it. The default
executor is:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">default_executor</span><span class="p">(</span><span class="n">function</span><span class="p">):</span>
<span class="k">return</span> <span class="n">function</span><span class="p">()</span>
</pre></div>
</div>
<p>You define executors by defining a method execute_example on a class. Any
test methods on that class with <a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">@given</span></code></a> used on them will use
<code class="docutils literal"><span class="pre">self.execute_example</span></code> as an executor with which to run tests. For example,
the following executor runs all its code twice:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">unittest</span> <span class="k">import</span> <span class="n">TestCase</span>
<span class="k">class</span> <span class="nc">TestTryReallyHard</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test_something</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="n">perform_some_unreliable_operation</span><span class="p">(</span><span class="n">i</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">execute_example</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="n">f</span><span class="p">()</span>
<span class="k">return</span> <span class="n">f</span><span class="p">()</span>
</pre></div>
</div>
<p>Note: The functions you use in map, etc. will run <em>inside</em> the executor. i.e.
they will not be called until you invoke the function passed to <code class="docutils literal"><span class="pre">execute_example</span></code>.</p>
<p>An executor must be able to handle being passed a function which returns None,
otherwise it won’t be able to run normal test cases. So for example the following
executor is invalid:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">unittest</span> <span class="k">import</span> <span class="n">TestCase</span>
<span class="k">class</span> <span class="nc">TestRunTwice</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">execute_example</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="k">return</span> <span class="n">f</span><span class="p">()()</span>
</pre></div>
</div>
<p>and should be rewritten as:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">unittest</span> <span class="k">import</span> <span class="n">TestCase</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="k">class</span> <span class="nc">TestRunTwice</span><span class="p">(</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span> <span class="nf">execute_example</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">f</span><span class="p">()</span>
<span class="k">if</span> <span class="n">inspect</span><span class="o">.</span><span class="n">isfunction</span><span class="p">(</span><span class="n">result</span><span class="p">):</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">result</span><span class="p">()</span>
<span class="k">return</span> <span class="n">result</span>
</pre></div>
</div>
</div>
<div class="section" id="using-hypothesis-to-find-values">
<h2>Using Hypothesis to find values<a class="headerlink" href="#using-hypothesis-to-find-values" title="Permalink to this headline">¶</a></h2>
<p>You can use Hypothesis’s data exploration features to find values satisfying
some predicate. This is generally useful for exploring custom strategies
defined with <a class="reference internal" href="data.html#hypothesis.strategies.composite" title="hypothesis.strategies.composite"><code class="xref py py-func docutils literal"><span class="pre">@composite</span></code></a>, or
experimenting with conditions for filtering data.</p>
<dl class="function">
<dt id="hypothesis.find">
<code class="descclassname">hypothesis.</code><code class="descname">find</code><span class="sig-paren">(</span><em>specifier</em>, <em>condition</em>, <em>settings=None</em>, <em>random=None</em>, <em>database_key=None</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/core.html#find"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.find" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the minimal example from the given strategy <code class="docutils literal"><span class="pre">specifier</span></code> that
matches the predicate function <code class="docutils literal"><span class="pre">condition</span></code>.</p>
</dd></dl>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hypothesis</span> <span class="k">import</span> <span class="n">find</span>
<span class="gp">>>> </span><span class="kn">from</span> <span class="nn">hypothesis.strategies</span> <span class="k">import</span> <span class="n">sets</span><span class="p">,</span> <span class="n">lists</span><span class="p">,</span> <span class="n">integers</span>
<span class="gp">>>> </span><span class="n">find</span><span class="p">(</span><span class="n">lists</span><span class="p">(</span><span class="n">integers</span><span class="p">()),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">10</span><span class="p">)</span>
<span class="go">[10]</span>
<span class="gp">>>> </span><span class="n">find</span><span class="p">(</span><span class="n">lists</span><span class="p">(</span><span class="n">integers</span><span class="p">()),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">10</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">[0, 0, 10]</span>
<span class="gp">>>> </span><span class="n">find</span><span class="p">(</span><span class="n">sets</span><span class="p">(</span><span class="n">integers</span><span class="p">()),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">10</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">>=</span> <span class="mi">3</span><span class="p">)</span>
<span class="go">{0, 1, 9}</span>
</pre></div>
</div>
<p>The first argument to <a class="reference internal" href="#hypothesis.find" title="hypothesis.find"><code class="xref py py-func docutils literal"><span class="pre">find()</span></code></a> describes data in the usual way for an argument to
<a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">given()</span></code></a>, and supports <a class="reference internal" href="data.html"><span class="doc">all the same data types</span></a>. The second is a
predicate it must satisfy.</p>
<p>Of course not all conditions are satisfiable. If you ask Hypothesis for an
example to a condition that is always false it will raise an error:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">find</span><span class="p">(</span><span class="n">integers</span><span class="p">(),</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="kc">False</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="o">...</span>
<span class="gr">hypothesis.errors.NoSuchExample</span>: <span class="n">No examples of condition lambda x: <unknown></span>
</pre></div>
</div>
<p>(The <code class="docutils literal"><span class="pre">lambda</span> <span class="pre">x:</span> <span class="pre">unknown</span></code> is because Hypothesis can’t retrieve the source code
of lambdas from the interactive python console. It gives a better error message
most of the time which contains the actual condition)</p>
</div>
<div class="section" id="inferred-strategies">
<span id="type-inference"></span><h2>Inferred Strategies<a class="headerlink" href="#inferred-strategies" title="Permalink to this headline">¶</a></h2>
<p>In some cases, Hypothesis can work out what to do when you omit arguments.
This is based on introspection, <em>not</em> magic, and therefore has well-defined
limits.</p>
<p><a class="reference internal" href="data.html#hypothesis.strategies.builds" title="hypothesis.strategies.builds"><code class="xref py py-func docutils literal"><span class="pre">builds()</span></code></a> will check the signature of the
<code class="docutils literal"><span class="pre">target</span></code> (using <code class="xref py py-func docutils literal"><span class="pre">getfullargspec()</span></code>).
If there are required arguments with type annotations and
no strategy was passed to <a class="reference internal" href="data.html#hypothesis.strategies.builds" title="hypothesis.strategies.builds"><code class="xref py py-func docutils literal"><span class="pre">builds()</span></code></a>,
<a class="reference internal" href="data.html#hypothesis.strategies.from_type" title="hypothesis.strategies.from_type"><code class="xref py py-func docutils literal"><span class="pre">from_type()</span></code></a> is used to fill them in.
You can also pass the special value <code class="xref py py-const docutils literal"><span class="pre">hypothesis.infer</span></code> as a keyword
argument, to force this inference for arguments with a default value.</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">b</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
<span class="gp">... </span> <span class="k">return</span> <span class="p">[</span><span class="n">a</span><span class="p">,</span> <span class="n">b</span><span class="p">]</span>
<span class="gp">>>> </span><span class="n">builds</span><span class="p">(</span><span class="n">func</span><span class="p">)</span><span class="o">.</span><span class="n">example</span><span class="p">()</span>
<span class="go">[72627971792323936471739212691379790782, '']</span>
</pre></div>
</div>
<p><a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">@given</span></code></a> does not perform any implicit inference
for required arguments, as this would break compatibility with pytest fixtures.
<code class="xref py py-const docutils literal"><span class="pre">infer</span></code> can be used as a keyword argument to explicitly
fill in an argument from its type annotation.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="nd">@given</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="n">infer</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span> <span class="k">pass</span>
<span class="c1"># is equivalent to</span>
<span class="nd">@given</span><span class="p">(</span><span class="n">a</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
<span class="k">def</span> <span class="nf">test</span><span class="p">(</span><span class="n">a</span><span class="p">):</span> <span class="k">pass</span>
</pre></div>
</div>
<div class="section" id="limitations">
<h3>Limitations<a class="headerlink" href="#limitations" title="Permalink to this headline">¶</a></h3>
<p><span class="target" id="index-0"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-3107"><strong>PEP 3107</strong></a> type annotations are not supported on Python 2, and Hypothesis
does not inspect <span class="target" id="index-1"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> type comments at runtime. While
<a class="reference internal" href="data.html#hypothesis.strategies.from_type" title="hypothesis.strategies.from_type"><code class="xref py py-func docutils literal"><span class="pre">from_type()</span></code></a> will work as usual, inference in
<a class="reference internal" href="data.html#hypothesis.strategies.builds" title="hypothesis.strategies.builds"><code class="xref py py-func docutils literal"><span class="pre">builds()</span></code></a> and <a class="reference internal" href="#hypothesis.given" title="hypothesis.given"><code class="xref py py-func docutils literal"><span class="pre">@given</span></code></a>
will only work if you manually create the <code class="docutils literal"><span class="pre">__annotations__</span></code> attribute
(e.g. by using <code class="docutils literal"><span class="pre">@annotations(...)</span></code> and <code class="docutils literal"><span class="pre">@returns(...)</span></code> decorators).
The <code class="xref py py-mod docutils literal"><span class="pre">python:typing</span></code> module is fully supported on Python 2 if you have
the backport installed.</p>
<p>The <code class="xref py py-mod docutils literal"><span class="pre">python:typing</span></code> module is provisional and has a number of internal
changes between Python 3.5.0 and 3.6.1, including at minor versions. These
are all supported on a best-effort basis, but you may encounter problems with
an old version of the module. Please report them to us, and consider
updating to a newer version of Python as a workaround.</p>
</div>
</div>
</div>
</div>
<div class="articleComments">
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="settings.html" class="btn btn-neutral float-right" title="Settings" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="quickstart.html" class="btn btn-neutral" title="Quick start guide" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2013-2018, David R. MacIver.
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT:'./',
VERSION:'3.44.1',
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>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.StickyNav.enable();
});
</script>
</body>
</html>
|