This file is indexed.

/usr/share/doc/python-hypothesis/html/stateful.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
<!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>Stateful testing &mdash; 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="Compatibility" href="supported.html"/>
        <link rel="prev" title="The Hypothesis Example Database" href="database.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"><a class="reference internal" href="details.html">Details and advanced features</a></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 current"><a class="current reference internal" href="#">Stateful testing</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#rule-based-state-machines">Rule based state machines</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#preconditions">Preconditions</a></li>
<li class="toctree-l3"><a class="reference internal" href="#invariant">Invariant</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#generic-state-machines">Generic state machines</a></li>
<li class="toctree-l2"><a class="reference internal" href="#more-fine-grained-control">More fine grained control</a></li>
</ul>
</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> &raquo;</li>
        
      <li>Stateful testing</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
            <a href="_sources/stateful.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="stateful-testing">
<h1>Stateful testing<a class="headerlink" href="#stateful-testing" title="Permalink to this headline"></a></h1>
<p>Hypothesis offers support for a stateful style of test, where instead of
trying to produce a single data value that causes a specific test to fail, it
tries to generate a program that errors. In many ways, this sort of testing is
to classical property based testing as property based testing is to normal
example based testing.</p>
<p>The idea doesn’t originate with Hypothesis, though Hypothesis’s implementation
and approach is mostly not based on an existing implementation and should be
considered some mix of novel and independent reinventions.</p>
<p>This style of testing is useful both for programs which involve some sort
of mutable state and for complex APIs where there’s no state per se but the
actions you perform involve e.g. taking data from one function and feeding it
into another.</p>
<p>The idea is that you teach Hypothesis how to interact with your program: Be it
a server, a python API, whatever. All you need is to be able to answer the
question “Given what I’ve done so far, what could I do now?”. After that,
Hypothesis takes over and tries to find sequences of actions which cause a
test failure.</p>
<p>Right now the stateful testing is a bit new and experimental and should be
considered as a semi-public API: It may break between minor versions but won’t
break between patch releases, and there are still some rough edges in the API
that will need to be filed off.</p>
<p>This shouldn’t discourage you from using it. Although it’s not as robust as the
rest of Hypothesis, it’s still pretty robust and more importantly is extremely
powerful. I found a number of really subtle bugs in Hypothesis by turning the
stateful testing onto a subset of the Hypothesis API, and you likely will find
the same.</p>
<p>Enough preamble, lets see how to use it.</p>
<p>The first thing to note is that there are two levels of API: The low level
but more flexible API and the higher level rule based API which is both
easier to use and also produces a much better display of data due to its
greater structure. We’ll start with the more structured one.</p>
<div class="section" id="rule-based-state-machines">
<h2>Rule based state machines<a class="headerlink" href="#rule-based-state-machines" title="Permalink to this headline"></a></h2>
<p>Rule based state machines are the ones you’re most likely to want to use.
They’re significantly more user friendly and should be good enough for most
things you’d want to do.</p>
<p>A rule based state machine is a collection of functions (possibly with side
effects) which may depend on both values that Hypothesis can generate and
also on values that have resulted from previous function calls.</p>
<p>You define a rule based state machine as follows:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>

<span class="kn">from</span> <span class="nn">hypothesis</span> <span class="k">import</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="kn">from</span> <span class="nn">hypothesis.stateful</span> <span class="k">import</span> <span class="n">RuleBasedStateMachine</span><span class="p">,</span> <span class="n">Bundle</span><span class="p">,</span> <span class="n">rule</span>


<span class="n">Leaf</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Leaf&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;label&#39;</span><span class="p">,))</span>
<span class="n">Split</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Split&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="s1">&#39;right&#39;</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">BalancedTrees</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>
    <span class="n">trees</span> <span class="o">=</span> <span class="n">Bundle</span><span class="p">(</span><span class="s1">&#39;BinaryTree&#39;</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">x</span><span class="o">=</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">leaf</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">return</span> <span class="n">Leaf</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">left</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="n">trees</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">split</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Split</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">tree</span><span class="o">=</span><span class="n">trees</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">check_balanced</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">Leaf</span><span class="p">):</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> <span class="o">&lt;=</span> <span class="mi">1</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_balanced</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_balanced</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">Leaf</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="mi">1</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>
</pre></div>
</div>
<p>In this we declare a Bundle, which is a named collection of previously generated
values. We define two rules which put data onto this bundle - one which just
generates leaves with integer labels, the other of which takes two previously
generated values and returns a new one.</p>
<p>We can then integrate this into our test suite by getting a unittest TestCase
from it:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="n">TestTrees</span> <span class="o">=</span> <span class="n">BalancedTrees</span><span class="o">.</span><span class="n">TestCase</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>(these will also be picked up by py.test if you prefer to use that). Running
this we get:</p>
<div class="code bash highlight-default"><div class="highlight"><pre><span></span><span class="n">Step</span> <span class="c1">#1: v1 = leaf(x=0)</span>
<span class="n">Step</span> <span class="c1">#2: v2 = split(left=v1, right=v1)</span>
<span class="n">Step</span> <span class="c1">#3: v3 = split(left=v2, right=v1)</span>
<span class="n">Step</span> <span class="c1">#4: check_balanced(tree=v3)</span>
<span class="n">F</span>
<span class="o">======================================================================</span>
<span class="n">FAIL</span><span class="p">:</span> <span class="n">runTest</span> <span class="p">(</span><span class="n">hypothesis</span><span class="o">.</span><span class="n">stateful</span><span class="o">.</span><span class="n">BalancedTrees</span><span class="o">.</span><span class="n">TestCase</span><span class="p">)</span>
<span class="o">----------------------------------------------------------------------</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="k">assert</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> <span class="o">&lt;=</span> <span class="mi">1</span>
<span class="ne">AssertionError</span>
</pre></div>
</div>
<p>Note how it’s printed out a very short program that will demonstrate the
problem.</p>
<p>…the problem of course being that we’ve not actually written any code to
balance this tree at <em>all</em>, so of course it’s not balanced.</p>
<p>So lets balance some trees.</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>

<span class="kn">from</span> <span class="nn">hypothesis</span> <span class="k">import</span> <span class="n">strategies</span> <span class="k">as</span> <span class="n">st</span>
<span class="kn">from</span> <span class="nn">hypothesis.stateful</span> <span class="k">import</span> <span class="n">RuleBasedStateMachine</span><span class="p">,</span> <span class="n">Bundle</span><span class="p">,</span> <span class="n">rule</span>


<span class="n">Leaf</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Leaf&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;label&#39;</span><span class="p">,))</span>
<span class="n">Split</span> <span class="o">=</span> <span class="n">namedtuple</span><span class="p">(</span><span class="s1">&#39;Split&#39;</span><span class="p">,</span> <span class="p">(</span><span class="s1">&#39;left&#39;</span><span class="p">,</span> <span class="s1">&#39;right&#39;</span><span class="p">))</span>


<span class="k">class</span> <span class="nc">BalancedTrees</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>
    <span class="n">trees</span> <span class="o">=</span> <span class="n">Bundle</span><span class="p">(</span><span class="s1">&#39;BinaryTree&#39;</span><span class="p">)</span>
    <span class="n">balanced_trees</span> <span class="o">=</span> <span class="n">Bundle</span><span class="p">(</span><span class="s1">&#39;balanced BinaryTree&#39;</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">x</span><span class="o">=</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">leaf</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">return</span> <span class="n">Leaf</span><span class="p">(</span><span class="n">x</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">left</span><span class="o">=</span><span class="n">trees</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="n">trees</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">split</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Split</span><span class="p">(</span><span class="n">left</span><span class="p">,</span> <span class="n">right</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">tree</span><span class="o">=</span><span class="n">balanced_trees</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">check_balanced</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">Leaf</span><span class="p">):</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="nb">abs</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">))</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">,</span> \
                <span class="nb">repr</span><span class="p">(</span><span class="n">tree</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_balanced</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">check_balanced</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>

    <span class="nd">@rule</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">balanced_trees</span><span class="p">,</span> <span class="n">tree</span><span class="o">=</span><span class="n">trees</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">balance_tree</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">split_leaves</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">tree</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">size</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">Leaf</span><span class="p">):</span>
            <span class="k">return</span> <span class="mi">1</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">size</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">flatten</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">tree</span><span class="p">):</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">tree</span><span class="p">,</span> <span class="n">Leaf</span><span class="p">):</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">label</span><span class="p">,)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">left</span><span class="p">)</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">flatten</span><span class="p">(</span><span class="n">tree</span><span class="o">.</span><span class="n">right</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">split_leaves</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">leaves</span><span class="p">):</span>
        <span class="k">assert</span> <span class="n">leaves</span>
        <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">leaves</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">Leaf</span><span class="p">(</span><span class="n">leaves</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">mid</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">leaves</span><span class="p">)</span> <span class="o">//</span> <span class="mi">2</span>
            <span class="k">return</span> <span class="n">Split</span><span class="p">(</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">split_leaves</span><span class="p">(</span><span class="n">leaves</span><span class="p">[:</span><span class="n">mid</span><span class="p">]),</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">split_leaves</span><span class="p">(</span><span class="n">leaves</span><span class="p">[</span><span class="n">mid</span><span class="p">:]),</span>
            <span class="p">)</span>
</pre></div>
</div>
<p>We’ve now written a really noddy tree balancing implementation.  This takes
trees and puts them into a new bundle of data, and we only assert that things
in the balanced_trees bundle are actually balanced.</p>
<p>If you run this it will sit there silently for a while (you can turn on
<a class="reference internal" href="settings.html#verbose-output"><span class="std std-ref">verbose output</span></a> to get slightly more information about
what’s happening. debug will give you all the intermediate programs being run)
and then run, telling you your test has passed! Our balancing algorithm worked.</p>
<p>Now lets break it to make sure the test is still valid:</p>
<p>Changing the split to <code class="docutils literal"><span class="pre">mid</span> <span class="pre">=</span> <span class="pre">max(len(leaves)</span> <span class="pre">//</span> <span class="pre">3,</span> <span class="pre">1)</span></code> this should no longer
balance, which gives us the following counter-example:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="n">v1</span> <span class="o">=</span> <span class="n">leaf</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="n">v2</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">left</span><span class="o">=</span><span class="n">v1</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="n">v1</span><span class="p">)</span>
<span class="n">v3</span> <span class="o">=</span> <span class="n">balance_tree</span><span class="p">(</span><span class="n">tree</span><span class="o">=</span><span class="n">v1</span><span class="p">)</span>
<span class="n">v4</span> <span class="o">=</span> <span class="n">split</span><span class="p">(</span><span class="n">left</span><span class="o">=</span><span class="n">v2</span><span class="p">,</span> <span class="n">right</span><span class="o">=</span><span class="n">v2</span><span class="p">)</span>
<span class="n">v5</span> <span class="o">=</span> <span class="n">balance_tree</span><span class="p">(</span><span class="n">tree</span><span class="o">=</span><span class="n">v4</span><span class="p">)</span>
<span class="n">check_balanced</span><span class="p">(</span><span class="n">tree</span><span class="o">=</span><span class="n">v5</span><span class="p">)</span>
</pre></div>
</div>
<p>Note that the example could be shrunk further by deleting v3. Due to some
technical limitations, Hypothesis was unable to find that particular shrink.
In general it’s rare for examples produced to be long, but they won’t always be
minimal.</p>
<p>You can control the detailed behaviour with a settings object on the TestCase
(this is a normal hypothesis settings object using the defaults at the time
the TestCase class was first referenced). For example if you wanted to run
fewer examples with larger programs you could change the settings to:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="n">TestTrees</span><span class="o">.</span><span class="n">settings</span> <span class="o">=</span> <span class="n">settings</span><span class="p">(</span><span class="n">max_examples</span><span class="o">=</span><span class="mi">100</span><span class="p">,</span> <span class="n">stateful_step_count</span><span class="o">=</span><span class="mi">100</span><span class="p">)</span>
</pre></div>
</div>
<p>Which doubles the number of steps each program runs and halves the number of
runs relative to the example. settings.timeout will also be respected as usual.</p>
<div class="section" id="preconditions">
<h3>Preconditions<a class="headerlink" href="#preconditions" title="Permalink to this headline"></a></h3>
<p>While it’s possible to use <a class="reference internal" href="details.html#hypothesis.assume" title="hypothesis.assume"><code class="xref py py-func docutils literal"><span class="pre">assume()</span></code></a> in RuleBasedStateMachine rules, if you
use it in only a few rules you can quickly run into a situation where few or
none of your rules pass their assumptions. Thus, Hypothesis provides a
<a class="reference internal" href="#hypothesis.stateful.precondition" title="hypothesis.stateful.precondition"><code class="xref py py-func docutils literal"><span class="pre">precondition()</span></code></a> decorator to avoid this problem. The <a class="reference internal" href="#hypothesis.stateful.precondition" title="hypothesis.stateful.precondition"><code class="xref py py-func docutils literal"><span class="pre">precondition()</span></code></a>
decorator is used on <code class="docutils literal"><span class="pre">rule</span></code>-decorated functions, and must be given a function
that returns True or False based on the RuleBasedStateMachine instance.</p>
<dl class="function">
<dt id="hypothesis.stateful.precondition">
<code class="descclassname">hypothesis.stateful.</code><code class="descname">precondition</code><span class="sig-paren">(</span><em>precond</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/stateful.html#precondition"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.stateful.precondition" title="Permalink to this definition"></a></dt>
<dd><p>Decorator to apply a precondition for rules in a RuleBasedStateMachine.
Specifies a precondition for a rule to be considered as a valid step in the
state machine. The given function will be called with the instance of
RuleBasedStateMachine and should return True or False. Usually it will need
to look at attributes on that instance.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyTestMachine</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>
    <span class="n">state</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="nd">@precondition</span><span class="p">(</span><span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="nd">@rule</span><span class="p">(</span><span class="n">numerator</span><span class="o">=</span><span class="n">integers</span><span class="p">())</span>
    <span class="k">def</span> <span class="nf">divide_with</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">numerator</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">=</span> <span class="n">numerator</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span>
</pre></div>
</div>
<p>This is better than using assume in your rule since more valid rules
should be able to be run.</p>
</dd></dl>

<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">hypothesis.stateful</span> <span class="k">import</span> <span class="n">RuleBasedStateMachine</span><span class="p">,</span> <span class="n">rule</span><span class="p">,</span> <span class="n">precondition</span>

<span class="k">class</span> <span class="nc">NumberModifier</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>

    <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="nd">@rule</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">add_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="nd">@precondition</span><span class="p">(</span><span class="k">lambda</span> <span class="bp">self</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">)</span>
    <span class="nd">@rule</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">divide_with_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">/</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span>
</pre></div>
</div>
<p>By using <a class="reference internal" href="#hypothesis.stateful.precondition" title="hypothesis.stateful.precondition"><code class="xref py py-func docutils literal"><span class="pre">precondition()</span></code></a> here instead of <a class="reference internal" href="details.html#hypothesis.assume" title="hypothesis.assume"><code class="xref py py-func docutils literal"><span class="pre">assume()</span></code></a>, Hypothesis can filter the
inapplicable rules before running them. This makes it much more likely that a
useful sequence of steps will be generated.</p>
<p>Note that currently preconditions can’t access bundles; if you need to use
preconditions, you should store relevant data on the instance instead.</p>
</div>
<div class="section" id="invariant">
<h3>Invariant<a class="headerlink" href="#invariant" title="Permalink to this headline"></a></h3>
<p>Often there are invariants that you want to ensure are met after every step in
a process.  It would be possible to add these as rules that are run, but they
would be run zero or multiple times between other rules. Hypothesis provides a
decorator that marks a function to be run after every step.</p>
<dl class="function">
<dt id="hypothesis.stateful.invariant">
<code class="descclassname">hypothesis.stateful.</code><code class="descname">invariant</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/hypothesis/stateful.html#invariant"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#hypothesis.stateful.invariant" title="Permalink to this definition"></a></dt>
<dd><p>Decorator to apply an invariant for rules in a RuleBasedStateMachine.
The decorated function will be run after every rule and can raise an
exception to indicate failed invariants.</p>
<p>For example:</p>
<div class="highlight-default"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">MyTestMachine</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>
    <span class="n">state</span> <span class="o">=</span> <span class="mi">1</span>

    <span class="nd">@invariant</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">is_nonzero</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">state</span> <span class="o">!=</span> <span class="mi">0</span>
</pre></div>
</div>
</dd></dl>

<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">hypothesis.stateful</span> <span class="k">import</span> <span class="n">RuleBasedStateMachine</span><span class="p">,</span> <span class="n">rule</span><span class="p">,</span> <span class="n">invariant</span>

<span class="k">class</span> <span class="nc">NumberModifier</span><span class="p">(</span><span class="n">RuleBasedStateMachine</span><span class="p">):</span>

    <span class="n">num</span> <span class="o">=</span> <span class="mi">0</span>

    <span class="nd">@rule</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">add_two</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">+=</span> <span class="mi">2</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">&gt;</span> <span class="mi">50</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">+=</span> <span class="mi">1</span>

    <span class="nd">@invariant</span><span class="p">()</span>
    <span class="k">def</span> <span class="nf">divide_with_one</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">assert</span> <span class="bp">self</span><span class="o">.</span><span class="n">num</span> <span class="o">%</span> <span class="mi">2</span> <span class="o">==</span> <span class="mi">0</span>

<span class="n">NumberTest</span> <span class="o">=</span> <span class="n">NumberModifier</span><span class="o">.</span><span class="n">TestCase</span>
</pre></div>
</div>
<p>Invariants can also have <a class="reference internal" href="#hypothesis.stateful.precondition" title="hypothesis.stateful.precondition"><code class="xref py py-func docutils literal"><span class="pre">precondition()</span></code></a>s applied to them, in which case
they will only be run if the precondition function returns true.</p>
<p>Note that currently invariants can’t access bundles; if you need to use
invariants, you should store relevant data on the instance instead.</p>
</div>
</div>
<div class="section" id="generic-state-machines">
<h2>Generic state machines<a class="headerlink" href="#generic-state-machines" title="Permalink to this headline"></a></h2>
<p>The class GenericStateMachine is the underlying machinery of stateful testing
in Hypothesis. In execution it looks much like the RuleBasedStateMachine but
it allows the set of steps available to depend in essentially arbitrary
ways on what has happened so far. For example, if you wanted to
use Hypothesis to test a game, it could choose each step in the machine based
on the game to date and the set of actions the game program is telling it it
has available.</p>
<p>It essentially executes the following loop:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="n">machine</span> <span class="o">=</span> <span class="n">MyStateMachine</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">machine</span><span class="o">.</span><span class="n">check_invariants</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">_</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="n">n_steps</span><span class="p">):</span>
        <span class="n">step</span> <span class="o">=</span> <span class="n">machine</span><span class="o">.</span><span class="n">steps</span><span class="p">()</span><span class="o">.</span><span class="n">example</span><span class="p">()</span>
        <span class="n">machine</span><span class="o">.</span><span class="n">execute_step</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
        <span class="n">machine</span><span class="o">.</span><span class="n">check_invariants</span><span class="p">()</span>
<span class="k">finally</span><span class="p">:</span>
    <span class="n">machine</span><span class="o">.</span><span class="n">teardown</span><span class="p">()</span>
</pre></div>
</div>
<p>Where <code class="docutils literal"><span class="pre">steps</span></code> and <code class="docutils literal"><span class="pre">execute_step</span></code> are methods you must implement, and
<code class="docutils literal"><span class="pre">teardown</span></code> and <code class="docutils literal"><span class="pre">check_invarants</span></code> are methods you can implement if required.
<code class="docutils literal"><span class="pre">steps</span></code> returns a strategy, which is allowed to depend arbitrarily on the
current state of the test execution. <em>Ideally</em> a good steps implementation
should be robust against minor changes in the state. Steps that change a lot
between slightly different executions will tend to produce worse quality
examples because they’re hard to simplify.</p>
<p>The steps method <em>may</em> depend on external state, but it’s not advisable and
may produce flaky tests.</p>
<p>If any of <code class="docutils literal"><span class="pre">execute_step</span></code>, <code class="docutils literal"><span class="pre">check_invariants</span></code> or <code class="docutils literal"><span class="pre">teardown</span></code> produces an
exception, Hypothesis will try to find a minimal sequence of values steps such
that the following throws an exception:</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="n">machine</span> <span class="o">=</span> <span class="n">MyStateMachine</span><span class="p">()</span>
<span class="k">try</span><span class="p">:</span>
    <span class="n">machine</span><span class="o">.</span><span class="n">check_invariants</span><span class="p">()</span>
    <span class="k">for</span> <span class="n">step</span> <span class="ow">in</span> <span class="n">steps</span><span class="p">:</span>
        <span class="n">machine</span><span class="o">.</span><span class="n">execute_step</span><span class="p">(</span><span class="n">step</span><span class="p">)</span>
        <span class="n">machine</span><span class="o">.</span><span class="n">check_invariants</span><span class="p">()</span>
<span class="k">finally</span><span class="p">:</span>
    <span class="n">machine</span><span class="o">.</span><span class="n">teardown</span><span class="p">()</span>
</pre></div>
</div>
<p>and such that at every point, the step executed is one that could plausible
have come from a call to <code class="docutils literal"><span class="pre">steps</span></code> in the current state.</p>
<p>Here’s an example of using stateful testing to test a broken implementation
of a set in terms of a list (note that you could easily do something close to
this example with the rule based testing instead, and probably should. This
is mostly for illustration purposes):</p>
<div class="code python highlight-default"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">unittest</span>

<span class="kn">from</span> <span class="nn">hypothesis.stateful</span> <span class="k">import</span> <span class="n">GenericStateMachine</span>
<span class="kn">from</span> <span class="nn">hypothesis.strategies</span> <span class="k">import</span> <span class="n">tuples</span><span class="p">,</span> <span class="n">sampled_from</span><span class="p">,</span> <span class="n">just</span><span class="p">,</span> <span class="n">integers</span>


<span class="k">class</span> <span class="nc">BrokenSet</span><span class="p">(</span><span class="n">GenericStateMachine</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">data</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="k">def</span> <span class="nf">steps</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">add_strategy</span> <span class="o">=</span> <span class="n">tuples</span><span class="p">(</span><span class="n">just</span><span class="p">(</span><span class="s2">&quot;add&quot;</span><span class="p">),</span> <span class="n">integers</span><span class="p">())</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">add_strategy</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span>
                <span class="n">add_strategy</span> <span class="o">|</span>
                <span class="n">tuples</span><span class="p">(</span><span class="n">just</span><span class="p">(</span><span class="s2">&quot;delete&quot;</span><span class="p">),</span> <span class="n">sampled_from</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="p">)))</span>

    <span class="k">def</span> <span class="nf">execute_step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">step</span><span class="p">):</span>
        <span class="n">action</span><span class="p">,</span> <span class="n">value</span> <span class="o">=</span> <span class="n">step</span>
        <span class="k">if</span> <span class="n">action</span> <span class="o">==</span> <span class="s1">&#39;delete&#39;</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">pass</span>
            <span class="k">assert</span> <span class="n">value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">assert</span> <span class="n">action</span> <span class="o">==</span> <span class="s1">&#39;add&#39;</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">data</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
            <span class="k">assert</span> <span class="n">value</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span>


<span class="n">TestSet</span> <span class="o">=</span> <span class="n">BrokenSet</span><span class="o">.</span><span class="n">TestCase</span>

<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">unittest</span><span class="o">.</span><span class="n">main</span><span class="p">()</span>
</pre></div>
</div>
<p>Note that the strategy changes each time based on the data that’s currently
in the state machine.</p>
<p>Running this gives us the following:</p>
<div class="code bash highlight-default"><div class="highlight"><pre><span></span><span class="n">Step</span> <span class="c1">#1: (&#39;add&#39;, 0)</span>
<span class="n">Step</span> <span class="c1">#2: (&#39;add&#39;, 0)</span>
<span class="n">Step</span> <span class="c1">#3: (&#39;delete&#39;, 0)</span>
<span class="n">F</span>
<span class="o">======================================================================</span>
<span class="n">FAIL</span><span class="p">:</span> <span class="n">runTest</span> <span class="p">(</span><span class="n">hypothesis</span><span class="o">.</span><span class="n">stateful</span><span class="o">.</span><span class="n">BrokenSet</span><span class="o">.</span><span class="n">TestCase</span><span class="p">)</span>
<span class="o">----------------------------------------------------------------------</span>
<span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="p">(</span><span class="o">...</span><span class="p">)</span>
    <span class="k">assert</span> <span class="n">value</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">data</span>
<span class="ne">AssertionError</span>
</pre></div>
</div>
<p>So it adds two elements, then deletes one, and throws an assertion when it
finds out that this only deleted one of the copies of the element.</p>
</div>
<div class="section" id="more-fine-grained-control">
<h2>More fine grained control<a class="headerlink" href="#more-fine-grained-control" title="Permalink to this headline"></a></h2>
<p>If you want to bypass the TestCase infrastructure you can invoke these
manually. The stateful module exposes the function run_state_machine_as_test,
which takes an arbitrary function returning a GenericStateMachine and an
optional settings parameter and does the same as the class based runTest
provided.</p>
<p>In particular this may be useful if you wish to pass parameters to a custom
__init__ in your subclass.</p>
</div>
</div>


           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="supported.html" class="btn btn-neutral float-right" title="Compatibility" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="database.html" class="btn btn-neutral" title="The Hypothesis Example Database" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; 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>