forked from UWPCE-PythonCert/ProgrammingInPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTesting_advanced.html
More file actions
867 lines (808 loc) · 74.4 KB
/
Copy pathTesting_advanced.html
File metadata and controls
867 lines (808 loc) · 74.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
<!DOCTYPE html>
<html class="writer-html5" lang="en" data-content_root="../">
<head>
<meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Advanced Testing — Programming in Python 8.0 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />
<link rel="stylesheet" type="text/css" href="../_static/css/theme.css?v=e59714d7" />
<script src="../_static/jquery.js?v=5d32c60e"></script>
<script src="../_static/_sphinx_javascript_frameworks_compat.js?v=2cd50e6c"></script>
<script src="../_static/documentation_options.js?v=bb3927b2"></script>
<script src="../_static/doctools.js?v=9bcbadda"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<script src="../_static/js/theme.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Introduction To Unit Testing" href="../exercises/unit_testing/unit_testing.html" />
<link rel="prev" title="Test Driven Development" href="TestDrivenDevelopment.html" />
</head>
<body class="wy-body-for-nav">
<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" style="background: #4b2e83" >
<a href="../index.html">
</a>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" aria-label="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="Navigation menu">
<p class="caption" role="heading"><span class="caption-text">Topics in the Program</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../topics/01-setting_up/index.html">1. Setting up your Environment</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/02-basic_python/index.html">2. Basic Python</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/03-recursion_booleans/index.html">3. Booleans and Recursion</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/04-sequences_iteration/index.html">4. Sequences and Iteration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/05-text_handling/index.html">5. Basic Text Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/06-exceptions/index.html">6. Exception Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/07-unit_testing/index.html">7. Unit Testing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/08-dicts_sets/index.html">8. Dictionaries and Sets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/09-files/index.html">9. File Handling</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/10-modules_packages/index.html">10. Modules and Packages</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/11-argument_passing/index.html">11. Advanced Argument Passing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/12-comprehensions/index.html">12. Comprehensions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/13-intro_oo/index.html">13. Intro to Object Oriented Programing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/14-magic_methods/index.html">14. Properties and Magic Methods</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/15-subclassing/index.html">15. Subclassing and Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/16-multiple_inheritance/index.html">16. Multiple Inheritance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../topics/17-functional_programming/index.html">17. Introduction to Functional Programming</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../topics/18-advanced_testing/index.html">18. Advanced Testing</a><ul class="current">
<li class="toctree-l2 current"><a class="current reference internal" href="#">Advanced Testing</a></li>
<li class="toctree-l2"><a class="reference internal" href="../exercises/mailroom/mailroom-mock.html">Mocking Mailroom</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../topics/99-extras/index.html">19. Extra Topics</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" style="background: #4b2e83" >
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../index.html">Programming in Python</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content style-external-links">
<div role="navigation" aria-label="Page navigation">
<ul class="wy-breadcrumbs">
<li><a href="../index.html" class="icon icon-home" aria-label="Home"></a></li>
<li class="breadcrumb-item"><a href="../topics/07-unit_testing/index.html"><span class="section-number">7. </span>Unit Testing</a></li>
<li class="breadcrumb-item active">Advanced Testing</li>
<li class="wy-breadcrumbs-aside">
<a href="../_sources/modules/Testing_advanced.rst.txt" rel="nofollow"> View page source</a>
</li>
</ul><div class="rst-breadcrumbs-buttons" role="navigation" aria-label="Sequential page navigation">
<a href="TestDrivenDevelopment.html" class="btn btn-neutral float-left" title="Test Driven Development" accesskey="p"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/unit_testing/unit_testing.html" class="btn btn-neutral float-right" title="Introduction To Unit Testing" accesskey="n">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<section id="advanced-testing">
<span id="id1"></span><h1>Advanced Testing<a class="headerlink" href="#advanced-testing" title="Link to this heading"></a></h1>
<p>Testing in Python</p>
<section id="what-is-testing">
<h2>What is testing?<a class="headerlink" href="#what-is-testing" title="Link to this heading"></a></h2>
<p>Code which runs your application in as close to a real environment as feasible and validates its behavior</p>
<section id="terminology-of-testing">
<h3>Terminology of testing<a class="headerlink" href="#terminology-of-testing" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p>Unit tests</p></li>
<li><p>Integration tests</p></li>
<li><p>High level system tests</p></li>
<li><p>Acceptance tests</p></li>
<li><p>Black box / White box testing</p></li>
</ul>
</section>
<section id="v-model-and-tests-levels">
<h3>“V” model and tests levels<a class="headerlink" href="#v-model-and-tests-levels" title="Link to this heading"></a></h3>
<img alt="../_images/test_v_model.png" src="../_images/test_v_model.png" />
</section>
<section id="unit-testing">
<h3>Unit testing<a class="headerlink" href="#unit-testing" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p>Test smallest discrete units of source code</p></li>
<li><p>Tests should be independent of each other</p></li>
<li><p>Can separate tests from required resources through fixtures and
mocking</p></li>
<li><p>Automatable</p></li>
<li><p>Integrates with the development process</p></li>
</ul>
</section>
<section id="what-should-be-tested">
<h3>What should be tested?<a class="headerlink" href="#what-should-be-tested" title="Link to this heading"></a></h3>
<p>The percentage of code which gets run in a test is known as the “coverage”.</p>
<p><strong>100% coverage is neither necessary nor sufficient</strong></p>
<p>100% coverage is an ideal to strive for.
But the decision on when and what to test should take into account the volatility of the project, and the difficulty of testing some edge cases / error conditions.</p>
<p><strong>NOTE</strong> Even if every line of code is run during tests (100% coverage),
they <em>will not</em> be comprehensive!
It is very hard to anticipate every weird input some code may get.</p>
</section>
<section id="unit-testing-tools">
<h3>Unit Testing tools<a class="headerlink" href="#unit-testing-tools" title="Link to this heading"></a></h3>
<ul>
<li><p>unittest, the test framework that ships with Python. Port of Java jUnit</p>
<p><a class="reference external" href="http://docs.python.org/3/library/unittest.html">http://docs.python.org/3/library/unittest.html</a></p>
</li>
<li><p>pytest, a test runner (it can run unittest <code class="docutils literal notranslate"><span class="pre">TestCase</span></code>, and is also an alternative to unittest.</p>
<p><a class="reference external" href="http://pytest.org/latest/">http://pytest.org/latest/</a></p>
</li>
<li><p>mock, an object mocking library. Ships with Python 3.3+</p>
<p><a class="reference external" href="https://docs.python.org/dev/library/unittest.mock.html">https://docs.python.org/dev/library/unittest.mock.html</a></p>
</li>
<li><p>coverage, a package for measuring test coverage.</p></li>
</ul>
<p>Note that while mock is in the <code class="docutils literal notranslate"><span class="pre">unittest</span></code> package, you do not need to be using <code class="docutils literal notranslate"><span class="pre">unittest</span></code> tests to use it.</p>
</section>
<section id="unittest">
<h3>unittest<a class="headerlink" href="#unittest" title="Link to this heading"></a></h3>
<p>The unittest framework comes with the standard library</p>
<p>Unittest is ported from Java’s jUnit – it is therefore OO-heavy, and
requires a lot of boilerplate code.</p>
<p>Many projects built custom testing Frameworks on top of it – e.g. Django</p>
<p>Therefore you will encounter it</p>
<p>So it’s good to be familiar with it.</p>
<p>Key missing features:</p>
<blockquote>
<div><ul class="simple">
<li><p>A test runner</p>
<ul>
<li><p>many people use nose or pytest to run unittest tests.</p></li>
</ul>
</li>
<li><p>Parameterized tests</p>
<ul>
<li><p>there are kludges and some third-party tools for this.</p></li>
</ul>
</li>
</ul>
</div></blockquote>
</section>
<section id="unittest-testcase-anatomy">
<h3>unittest.TestCase anatomy<a class="headerlink" href="#unittest-testcase-anatomy" title="Link to this heading"></a></h3>
<ul class="simple">
<li><p>create a new subclass of <code class="docutils literal notranslate"><span class="pre">unittest.TestCase</span></code></p></li>
<li><p>name test methods <code class="docutils literal notranslate"><span class="pre">test_foo</span></code> so the test runner finds them</p></li>
<li><p>make calls to the <code class="docutils literal notranslate"><span class="pre">self.assert*</span></code> family of methods to validate results</p></li>
</ul>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>
<span class="k">class</span><span class="w"> </span><span class="nc">TestMyStuff</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span><span class="w"> </span><span class="nf">setUp</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">x</span> <span class="o">=</span> <span class="mi">2</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_add</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">assertEqual</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">x</span><span class="o">+</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_len</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">assertEqual</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="s1">'foo'</span><span class="p">),</span> <span class="mi">3</span><span class="p">)</span>
<span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s1">'__main__'</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>
</section>
<section id="assert-methods">
<h3>Assert Methods<a class="headerlink" href="#assert-methods" title="Link to this heading"></a></h3>
<p>TestCase contains a number of methods named <code class="docutils literal notranslate"><span class="pre">assert*</span></code> which can be used
for validation, here are a few common ones:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">assertEqual</span><span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="n">second</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">assertNotEqual</span><span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="n">second</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">assertTrue</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">assertFalse</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span>
<span class="n">assertIn</span><span class="p">(</span><span class="n">first</span><span class="p">,</span> <span class="n">second</span><span class="p">)</span>
<span class="n">assertRaises</span><span class="p">(</span><span class="n">exc</span><span class="p">,</span> <span class="n">fun</span><span class="p">,</span> <span class="n">msg</span><span class="o">=</span><span class="kc">None</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>
</pre></div>
</div>
<p>See a full list at:</p>
<p><a class="reference external" href="http://docs.python.org/3/library/unittest.html#assert-methods">http://docs.python.org/3/library/unittest.html#assert-methods</a> or</p>
<p><code class="docutils literal notranslate"><span class="pre">dir(unittest.TestCase)</span></code> or to get really fancy</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="nb">print</span><span class="p">(</span><span class="n">i</span><span class="p">)</span> <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">)</span> <span class="k">if</span> <span class="n">i</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">'assert'</span><span class="p">)]</span>
</pre></div>
</div>
</section>
</section>
<section id="running-your-tests">
<h2>Running your tests<a class="headerlink" href="#running-your-tests" title="Link to this heading"></a></h2>
<p>How do you actually run your tests?</p>
<section id="running-tests-in-a-single-module">
<h3>running tests in a single module<a class="headerlink" href="#running-tests-in-a-single-module" title="Link to this heading"></a></h3>
<p>Call unittest.main() right in your module</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">if</span> <span class="vm">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</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>or from the command line:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>unittest<span class="w"> </span>test_my_module<span class="w"> </span><span class="c1"># with or without .py on end</span>
python<span class="w"> </span>-m<span class="w"> </span>unittest<span class="w"> </span>test_my_module.TestClass<span class="w"> </span><span class="c1"># particular class in a module</span>
python<span class="w"> </span>-m<span class="w"> </span>unittest<span class="w"> </span>test_my_module.TestClass.test_method<span class="w"> </span><span class="c1"># particular test</span>
</pre></div>
</div>
<p>If it gets cumbersome with many TestCases, organize the tests into a
test suite (or use a test runner, which we get to soon).</p>
</section>
<section id="test-suites">
<h3>Test Suites<a class="headerlink" href="#test-suites" title="Link to this heading"></a></h3>
<p>Test suites group test cases into a single testable unit</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">calculator_test</span><span class="w"> </span><span class="kn">import</span> <span class="n">TestCalculatorFunctions</span>
<span class="n">suite</span> <span class="o">=</span> <span class="n">unittest</span><span class="o">.</span><span class="n">TestLoader</span><span class="p">()</span><span class="o">.</span><span class="n">loadTestsFromTestCase</span><span class="p">(</span><span class="n">TestCalculatorFunctions</span><span class="p">)</span>
<span class="n">unittest</span><span class="o">.</span><span class="n">TextTestRunner</span><span class="p">(</span><span class="n">verbosity</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span><span class="o">.</span><span class="n">run</span><span class="p">(</span><span class="n">suite</span><span class="p">)</span>
</pre></div>
</div>
<p>Tests can also be organized into suites in the</p>
<p><code class="docutils literal notranslate"><span class="pre">if</span> <span class="pre">__name__</span> <span class="pre">==</span> <span class="pre">"__main__":</span></code></p>
<p>block</p>
</section>
<section id="testrunner-pytest">
<h3>TestRunner: pytest<a class="headerlink" href="#testrunner-pytest" title="Link to this heading"></a></h3>
<p>pytest is a very widely used and comprehensive test runner: it can auto-discover test cases, run them, and report of the results.</p>
<p>NOTE: Way back in the day, there was a test runner called “nose” – there is some chance you may still see it used in older projects. It’s similar to, but with fewer features than pytest.</p>
<p>It will find tests for you so you can focus on writing tests, not
maintaining test suites.</p>
<p>To find tests, pytest looks for modules (such as python files)
whose names start with ‘test’. In those modules, they will load tests
from all unittest.TestCase subclasses, as well as functions whose names
start with ‘test’.</p>
<p>So running your tests is as easy as</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pytest
</pre></div>
</div>
<p>or</p>
<p><a class="reference external" href="https://docs.pytest.org/en/latest/index.html">https://docs.pytest.org/en/latest/index.html</a></p>
</section>
<section id="fixtures-setting-up-your-tests-for-success">
<h3>Fixtures: Setting up your tests for success<a class="headerlink" href="#fixtures-setting-up-your-tests-for-success" title="Link to this heading"></a></h3>
<p>(or failure!)</p>
<p>Test fixtures are a fixed baseline for tests to run from consistently,
also known as test context.</p>
<p>Fixtures can (and should) be set up fresh before each test, once before each test case, or before an entire test suite.</p>
</section>
<section id="fixtures-in-unittest">
<h3>Fixtures in unittest<a class="headerlink" href="#fixtures-in-unittest" title="Link to this heading"></a></h3>
<p>unittest provides fixture support via these methods:</p>
<ul class="simple">
<li><p>setUp / tearDown - these are run before and after each test method</p></li>
<li><p>setUpClass / tearDownClass - these are run before/after each TestCase</p></li>
<li><p>setUpModule / tearDownModule - run before/after each TestSuite</p></li>
<li><p>addCleanup / doCleanups - called after tearDown,
in case a test throws an exception</p></li>
</ul>
</section>
<section id="fixtures-in-pytest">
<h3>Fixtures in pytest<a class="headerlink" href="#fixtures-in-pytest" title="Link to this heading"></a></h3>
<p>pytest provides a fixture system that is powerful and flexible:</p>
<p><a class="reference external" href="https://docs.pytest.org/en/latest/fixture.html#fixture">https://docs.pytest.org/en/latest/fixture.html#fixture</a></p>
<p>You use a decorator to create a fixture:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">pytest</span>
<span class="nd">@pytest</span><span class="o">.</span><span class="n">fixture</span>
<span class="k">def</span><span class="w"> </span><span class="nf">smtp</span><span class="p">():</span>
<span class="kn">import</span><span class="w"> </span><span class="nn">smtplib</span>
<span class="k">return</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s2">"smtp.gmail.com"</span><span class="p">)</span>
</pre></div>
</div>
<p>A fixture is simply a function that will get run when it is used, and
returns <em>something</em> that your tests need:</p>
<p>To use a fixture, you add it as a parameter to your test function:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">test_ehlo</span><span class="p">(</span><span class="n">smtp</span><span class="p">):</span>
<span class="n">response</span><span class="p">,</span> <span class="n">msg</span> <span class="o">=</span> <span class="n">smtp</span><span class="o">.</span><span class="n">ehlo</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">response</span> <span class="o">==</span> <span class="mi">250</span>
<span class="k">assert</span> <span class="mi">0</span> <span class="c1"># for demo purposes</span>
</pre></div>
</div>
<p>The parameter gets set to the value returned by the fixture function.
The fixture function is automatically run before each test.</p>
<p>Let’s see this in action:</p>
<p><a class="reference download internal" download="" href="../_downloads/cd1f82fcc00d02d1af7aae379d84f37e/pytest_fixtures.py"><code class="xref download docutils literal notranslate"><span class="pre">pytest_fixtures.py</span></code></a></p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pytest<span class="w"> </span>-s<span class="w"> </span>-v<span class="w"> </span>pytest_fixtures.py
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">-s</span></code> tells pytest not to capture stdout – so we can see print statements.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">-v</span></code> is verbose mode – so we can see a bit more what is going on.</p>
</section>
<section id="teardown">
<h3>“Teardown”<a class="headerlink" href="#teardown" title="Link to this heading"></a></h3>
<p>If your fixture needs to clean itself up after its done, this is known as
“teardown”</p>
<p>To accomplish this in pytest, you use “yield”, rather than “return”.</p>
<p>The teardown code will run after the yield</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@pytest</span><span class="o">.</span><span class="n">fixture</span>
<span class="k">def</span><span class="w"> </span><span class="nf">smtp</span><span class="p">(</span><span class="n">request</span><span class="p">):</span>
<span class="n">smtp</span> <span class="o">=</span> <span class="n">smtplib</span><span class="o">.</span><span class="n">SMTP</span><span class="p">(</span><span class="s2">"smtp.gmail.com"</span><span class="p">)</span>
<span class="k">yield</span> <span class="n">smtp</span> <span class="c1"># provide the fixture value</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"teardown smtp"</span><span class="p">)</span>
<span class="n">smtp</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
<p>Remember that putting a yield in a function makes it a generator function – which provides a way to halt execution of the function, return a value, and then pick up where it left off. So in this case, you use whatever code you want to generate your object – then after the yield, all those variables will be there, so you can do whatever clean up you need to do.</p>
<p>See the example again for this…</p>
</section>
</section>
<section id="testing-floating-point-values">
<h2>Testing floating point values<a class="headerlink" href="#testing-floating-point-values" title="Link to this heading"></a></h2>
<p>Why can’t we just test if .5 == 1/2 ?</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [1]: </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">.15</span> <span class="o">==</span> <span class="mf">.45</span>
<span class="gh">Out[1]: </span><span class="go">False</span>
<span class="gp">In [2]: </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">.15</span>
<span class="gh">Out[2]: </span><span class="go">0.44999999999999996</span>
<span class="gp">In [3]: </span><span class="mi">3</span> <span class="o">*</span> <span class="mf">.15</span> <span class="o">*</span> <span class="mi">10</span> <span class="o">/</span> <span class="mi">10</span> <span class="o">==</span> <span class="mf">.45</span>
<span class="gh">Out[3]: </span><span class="go">True</span>
</pre></div>
</div>
<p>There are an infinite number of real numbers, so they are
stored as an approximation in computing hardware.</p>
<p><a class="reference external" href="https://docs.python.org/3/tutorial/floatingpoint.html">https://docs.python.org/3/tutorial/floatingpoint.html</a></p>
<section id="levels-of-precision-of-floating-point">
<h3>Levels of precision of floating point<a class="headerlink" href="#levels-of-precision-of-floating-point" title="Link to this heading"></a></h3>
<p>Python floating point numbers are stored in <a class="reference external" href="http://en.wikipedia.org/wiki/IEEE_floating_point">IEEE 754</a> 64-bit double precision format, so 1 bit for the sign, 11 bits for the exponent, and the remaining 52 for the fraction.</p>
<p>So we can count on up to about 16 digits of precision in decimal:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="nb">len</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="mi">2</span><span class="o">**</span><span class="mi">52</span><span class="p">))</span>
<span class="gh">Out[39]: </span><span class="go">16</span>
<span class="gp">In [40]: </span><span class="mf">.1</span><span class="o">+</span><span class="mf">.2</span>
<span class="gh">Out[40]: </span><span class="go">0.30000000000000004</span>
<span class="gp">In [41]: </span><span class="nb">len</span><span class="p">(</span><span class="s1">'3000000000000000'</span><span class="p">)</span>
<span class="gh">Out[41]: </span><span class="go">16</span>
<span class="go"># with repeated operations, the errors eventually build up:</span>
<span class="go"># here's multiplying by "1" 10 million times:</span>
<span class="gp">In [64]: </span><span class="n">x</span><span class="o">=</span><span class="mi">1</span>
<span class="gp">In [69]: </span><span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">10000000</span><span class="p">):</span> <span class="n">x</span> <span class="o">*=</span> <span class="p">(</span><span class="mf">.1</span> <span class="o">+</span> <span class="mf">.2</span><span class="p">)</span><span class="o">/</span><span class="mf">.3</span>
<span class="go">Out [69]: 1.000000002220446</span>
</pre></div>
</div>
</section>
<section id="assertalmostequal">
<h3>assertAlmostEqual<a class="headerlink" href="#assertalmostequal" title="Link to this heading"></a></h3>
<p>assertAlmostEqual is a custom assert in <code class="docutils literal notranslate"><span class="pre">unittest</span></code> that verifies that two floating point values are close enough to each other.</p>
<p>Add a places keyword argument to specify the number of decimal places.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>
<span class="k">class</span><span class="w"> </span><span class="nc">TestAlmostEqual</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span><span class="w"> </span><span class="nf">setUp</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">pass</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_floating_point</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">assertEqual</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="mf">.15</span><span class="p">,</span> <span class="mf">.45</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_almost_equal</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">assertAlmostEqual</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="mf">.15</span><span class="p">,</span> <span class="mf">.45</span><span class="p">,</span> <span class="n">places</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="what-is-close-enough">
<h3>What is close enough?<a class="headerlink" href="#what-is-close-enough" title="Link to this heading"></a></h3>
<p><strong>Warning</strong></p>
<p><code class="docutils literal notranslate"><span class="pre">assertAlmostEqual</span></code> lets you specify <em>decimal places</em>, i.e. the number of digits after the decimal point.</p>
<p>This works great for numbers that are about magnitude 1.0 (as above)</p>
<p>But what if you have numbers that are very large? (or small):</p>
<blockquote>
<div><ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">1.0e22</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">1.0000000000001e22</span></code></p></li>
</ul>
</div></blockquote>
<p>are they almost equal?</p>
<p>Remember that python floating point numbers store the exponent and up
to 16 decimal digits.</p>
<p>So those two are almost as close as you can get. But:</p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [30]: </span><span class="n">x</span> <span class="o">=</span> <span class="mf">1e22</span>
<span class="gp">In [31]: </span><span class="n">y</span> <span class="o">=</span> <span class="mf">1.0000000000001e22</span>
<span class="gp">In [32]: </span><span class="s1">'</span><span class="si">%g</span><span class="s1">'</span><span class="o">%</span><span class="p">(</span><span class="n">y</span> <span class="o">-</span> <span class="n">x</span><span class="p">)</span>
<span class="gh">Out[32]: </span><span class="go">'1.00034e+09'</span>
</pre></div>
</div>
<p>They are different by about a billion!</p>
<p>In general, we don’t want to compare floating point numbers to within a
certain number of decimal places.</p>
<p>Anyone remember “significant figures” from science classes?</p>
</section>
<section id="isclose">
<h3><code class="docutils literal notranslate"><span class="pre">isclose()</span></code><a class="headerlink" href="#isclose" title="Link to this heading"></a></h3>
<p>Python 3.5 introduced the <code class="docutils literal notranslate"><span class="pre">isclose()</span></code> function in the <code class="docutils literal notranslate"><span class="pre">math</span></code> module:</p>
<p><a class="reference external" href="https://www.python.org/dev/peps/pep-0485/">https://www.python.org/dev/peps/pep-0485/</a></p>
<div class="highlight-ipython notranslate"><div class="highlight"><pre><span></span><span class="gp">In [39]: </span><span class="kn">import</span><span class="w"> </span><span class="nn">math</span>
<span class="gp">In [40]: </span><span class="n">x</span>
<span class="gh">Out[40]: </span><span class="go">1e+22</span>
<span class="gp">In [41]: </span><span class="n">y</span>
<span class="gh">Out[41]: </span><span class="go">1.0000000000001e+22</span>
<span class="gp">In [42]: </span><span class="n">math</span><span class="o">.</span><span class="n">isclose</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="gh">Out[42]: </span><span class="go">True</span>
</pre></div>
</div>
<p>So this works for any magnitude number.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">is_close</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="o">*</span><span class="p">,</span> <span class="n">rel_tol</span><span class="o">=</span><span class="mf">1e-09</span><span class="p">,</span> <span class="n">abs_tol</span><span class="o">=</span><span class="mf">0.0</span><span class="p">)</span> <span class="o">-></span> <span class="nb">bool</span>
<span class="n">Determine</span> <span class="n">whether</span> <span class="n">two</span> <span class="n">floating</span> <span class="n">point</span> <span class="n">numbers</span> <span class="n">are</span> <span class="n">close</span> <span class="ow">in</span> <span class="n">value</span><span class="o">.</span>
<span class="n">rel_tol</span>
<span class="n">maximum</span> <span class="n">difference</span> <span class="k">for</span> <span class="n">being</span> <span class="n">considered</span> <span class="s2">"close"</span><span class="p">,</span> <span class="n">relative</span> <span class="n">to</span> <span class="n">the</span>
<span class="n">magnitude</span> <span class="n">of</span> <span class="n">the</span> <span class="nb">input</span> <span class="n">values</span>
<span class="n">abs_tol</span>
<span class="n">maximum</span> <span class="n">difference</span> <span class="k">for</span> <span class="n">being</span> <span class="n">considered</span> <span class="s2">"close"</span><span class="p">,</span> <span class="n">regardless</span> <span class="n">of</span> <span class="n">the</span>
<span class="n">magnitude</span> <span class="n">of</span> <span class="n">the</span> <span class="nb">input</span> <span class="n">values</span>
<span class="n">Return</span> <span class="kc">True</span> <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="n">close</span> <span class="ow">in</span> <span class="n">value</span> <span class="n">to</span> <span class="n">b</span><span class="p">,</span> <span class="ow">and</span> <span class="kc">False</span> <span class="n">otherwise</span><span class="o">.</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">rel_tol</span></code> essentially specifies how many significant figures you want:
<code class="docutils literal notranslate"><span class="pre">1e-09</span></code> is 9 significant figures: about half of what floats can store.</p>
<p><code class="docutils literal notranslate"><span class="pre">abs_tol</span></code> is required for comparisons to zero – nothing is
“relatively close” to zero</p>
</section>
<section id="using-isclose-with-unittest">
<h3>Using <code class="docutils literal notranslate"><span class="pre">isclose()</span></code> with <code class="docutils literal notranslate"><span class="pre">unittest</span></code><a class="headerlink" href="#using-isclose-with-unittest" title="Link to this heading"></a></h3>
<p>Ideally, <code class="docutils literal notranslate"><span class="pre">TestCase</span></code> would have an <code class="docutils literal notranslate"><span class="pre">assertIsClose</span></code> method.
But you can use:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">unittest</span>
<span class="kn">from</span><span class="w"> </span><span class="nn">math</span><span class="w"> </span><span class="kn">import</span> <span class="n">isclose</span>
<span class="k">class</span><span class="w"> </span><span class="nc">TestAlmostEqual</span><span class="p">(</span><span class="n">unittest</span><span class="o">.</span><span class="n">TestCase</span><span class="p">):</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_floating_point</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">assertEqual</span><span class="p">(</span><span class="mi">3</span><span class="o">*</span><span class="mf">.15</span><span class="p">,</span> <span class="mf">.45</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_almost_equal</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">assertTrue</span><span class="p">(</span> <span class="n">isclose</span><span class="p">(</span> <span class="mi">3</span><span class="o">*</span><span class="mf">.15</span><span class="p">,</span> <span class="mf">.45</span><span class="p">,</span> <span class="n">rel_tol</span><span class="o">=</span><span class="mi">7</span><span class="p">)</span> <span class="p">)</span>
</pre></div>
</div>
<p><strong>NOTE</strong> This is one of the key flaws with the unittest module: while
it can test anything with <code class="docutils literal notranslate"><span class="pre">assertTrue</span></code> and the like – if there is no
nifty <code class="docutils literal notranslate"><span class="pre">assert*</span></code> method for your use-case, you lose the advantages of
the <code class="docutils literal notranslate"><span class="pre">assert*</span></code> methods.</p>
<p>What are those advantages? – mostly a prettier printing of information
in the error:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">FAIL</span><span class="p">:</span> <span class="n">test_floating_point</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestAlmostEqual</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="n">File</span> <span class="s2">"/Users/Chris/PythonStuff/UWPCE/Py300-Spring2017/Examples/testing/test_floats.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">17</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_floating_point</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="mi">3</span> <span class="o">*</span> <span class="mf">.15</span><span class="p">,</span> <span class="mf">.45</span><span class="p">)</span>
<span class="ne">AssertionError</span><span class="p">:</span> <span class="mf">0.44999999999999996</span> <span class="o">!=</span> <span class="mf">0.45</span>
</pre></div>
</div>
<p>But when you use assertTrue:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">FAIL</span><span class="p">:</span> <span class="n">test_isclose_tiny</span> <span class="p">(</span><span class="n">__main__</span><span class="o">.</span><span class="n">TestAlmostEqual</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="n">File</span> <span class="s2">"/Users/Chris/PythonStuff/UWPCE/Py300-Spring2017/Examples/testing/test_floats.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">32</span><span class="p">,</span> <span class="ow">in</span> <span class="n">test_isclose_tiny</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertTrue</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">isclose</span><span class="p">(</span><span class="mi">4</span> <span class="o">*</span> <span class="mf">.15e-30</span><span class="p">,</span> <span class="mf">.45e-30</span><span class="p">))</span>
<span class="ne">AssertionError</span><span class="p">:</span> <span class="kc">False</span> <span class="ow">is</span> <span class="ow">not</span> <span class="n">true</span>
</pre></div>
</div>
<p>Not that helpful – is it? I think we all already know that False is not true ;-)</p>
<p><code class="docutils literal notranslate"><span class="pre">pytest</span></code> give you nice informative messages when tests fail – without special asserts.</p>
</section>
</section>
<section id="parameterized-tests">
<h2>Parameterized Tests<a class="headerlink" href="#parameterized-tests" title="Link to this heading"></a></h2>
<p>Often you want to run exactly the same tests, but with different outputs and inputs.</p>
<p>You can do this in a really naive way, by putting multiple asserts into one test:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">test_multiply</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">multiply</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="o">==</span> <span class="mi">4</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">4</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">6</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
</pre></div>
</div>
<p>If they all pass, fine, but if not, it will fail on the first one,
and you’ll have no idea if the others pass.</p>
<p>Plus, it gets a bit tedious to write, particularly if the code is more
complex than a single function call.</p>
<p>You can write a separate test for each case:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">test_multiply_both_positive</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">multiply</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="o">==</span> <span class="mi">4</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_multiply_one_negative</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">==</span> <span class="o">-</span><span class="mi">4</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_multiply_both_negative</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">6</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_multiply_second_zero</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_multiply_first_zero</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
</pre></div>
</div>
<p>But talk about tedious!!!</p>
<p>Unfortunately, <code class="docutils literal notranslate"><span class="pre">unittest</span></code> does not have a built-in way to solve this problem.
There is a nifty library called parameterized, which does solve it (and they spell parameterize correctly).
It works with unittest, and pytest.</p>
<p><a class="reference external" href="https://pypi.python.org/pypi/parameterized">https://pypi.python.org/pypi/parameterized</a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@parameterized</span><span class="p">([</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="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">8</span><span class="p">),</span>
<span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">1</span><span class="p">),</span>
<span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">9</span><span class="p">,</span> <span class="mi">0</span><span class="p">),])</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_pow</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">exponent</span><span class="p">,</span> <span class="n">expected</span><span class="p">):</span>
<span class="n">assert_equal</span><span class="p">(</span><span class="n">math</span><span class="o">.</span><span class="n">pow</span><span class="p">(</span><span class="n">base</span><span class="p">,</span> <span class="n">exponent</span><span class="p">),</span> <span class="n">expected</span><span class="p">)</span>
</pre></div>
</div>
<p>You will find many more examples on their website.</p>
<section id="pytest-mark-parametrize">
<h3><code class="docutils literal notranslate"><span class="pre">pytest.mark.parametrize</span></code><a class="headerlink" href="#pytest-mark-parametrize" title="Link to this heading"></a></h3>
<p>With pytest, you don’t need a third party library: as it provides a nifty built-in way to do it:</p>
<p><a class="reference external" href="https://docs.pytest.org/en/latest/parametrize.html#parametrize-basics">https://docs.pytest.org/en/latest/parametrize.html#parametrize-basics</a></p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">param_names</span> <span class="o">=</span> <span class="s2">"arg1, arg2, result"</span>
<span class="n">params</span> <span class="o">=</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="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">),</span>
<span class="p">(</span><span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="mi">4</span><span class="p">),</span>
<span class="p">]</span>
<span class="nd">@pytest</span><span class="o">.</span><span class="n">mark</span><span class="o">.</span><span class="n">parametrize</span><span class="p">(</span><span class="n">param_names</span><span class="p">,</span> <span class="n">params</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_multiply</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">,</span> <span class="n">result</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">multiply</span><span class="p">(</span><span class="n">arg1</span><span class="p">,</span> <span class="n">arg2</span><span class="p">)</span> <span class="o">==</span> <span class="n">result</span>
</pre></div>
</div>
<p>I find this very, very, useful.</p>
<p>See <a class="reference download internal" download="" href="../_downloads/5a5fdff5de7f7f1944ea29e74393d4b9/test_calculator_pytest.py"><code class="xref download docutils literal notranslate"><span class="pre">test_calculator_pytest.py</span></code></a></p>
</section>
<section id="code-coverage">
<h3>Code Coverage<a class="headerlink" href="#code-coverage" title="Link to this heading"></a></h3>
<p>“Coverage” is the fraction of your code that is run by your tests.
That is, how much code is “covered” by the tests.</p>
<p>It’s usually reported as a percentage of lines of code that were run.</p>
<p>If a line of code is <em>not</em> run in your tests – you can be pretty
sure it hasn’t been tested – so how do you know it works?</p>
<p>So 100% coverage is a good goal (though harder to achieve than you might think!)</p>
<p>Keep in mind that 100% coverage does <strong>NOT</strong> mean that your code is <em>fully</em> tested – you have no idea how many corner cases may not have been checked.</p>
<p>But it’s a good start.</p>
<p>And low coverage means that your code is very sparsely tested – not great!</p>
</section>
<section id="the-coverage-tool">
<h3>The coverage tool<a class="headerlink" href="#the-coverage-tool" title="Link to this heading"></a></h3>
<p><code class="docutils literal notranslate"><span class="pre">coverage.py</span></code> is a tool (written by Ned Batchelder) for checking code testing coverage in python:</p>
<p><a class="reference external" href="https://coverage.readthedocs.io">https://coverage.readthedocs.io</a></p>
<p>It can be installed with <code class="docutils literal notranslate"><span class="pre">pip</span></code> (or conda):</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>python<span class="w"> </span>-m<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>coverage
</pre></div>
</div>
<p>To run coverage on your test suite:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>coverage<span class="w"> </span>run<span class="w"> </span>my_program.py<span class="w"> </span>arg1<span class="w"> </span>arg2
</pre></div>
</div>
<p>This generates a .coverage file. To analyze it on the console:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>coverage<span class="w"> </span>report
</pre></div>
</div>
<p>Or you can generate an HTML report in the current directory:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>coverage<span class="w"> </span>html
</pre></div>
</div>
<p>To find out coverage across the standard library, add -L:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">-</span><span class="n">L</span><span class="p">,</span> <span class="o">--</span><span class="n">pylib</span> <span class="n">Measure</span> <span class="n">coverage</span> <span class="n">even</span> <span class="n">inside</span> <span class="n">the</span> <span class="n">Python</span> <span class="n">installed</span>
<span class="n">library</span><span class="p">,</span> <span class="n">which</span> <span class="n">isn</span><span class="s1">'t done by default.</span>
</pre></div>
</div>
</section>
<section id="branch-coverage">
<h3>Branch Coverage<a class="headerlink" href="#branch-coverage" title="Link to this heading"></a></h3>
<p>consider the following code:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">x</span> <span class="o">=</span> <span class="kc">False</span> <span class="c1"># 1</span>
<span class="k">if</span> <span class="n">x</span><span class="p">:</span> <span class="c1"># 2</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"in branch"</span><span class="p">)</span> <span class="c1"># 3</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">"out of branch"</span><span class="p">)</span> <span class="c1"># 4</span>
</pre></div>
</div>
<p>We want to make sure the branch is being bypassed correctly in the False
case</p>
<p>Track which branch destinations were not visited with the –branch
option to run:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>coverage<span class="w"> </span>run<span class="w"> </span>--branch<span class="w"> </span>myprog.py
</pre></div>
</div>
<p><a class="reference external" href="http://nedbatchelder.com/code/coverage/branch.html">http://nedbatchelder.com/code/coverage/branch.html</a></p>
</section>
<section id="using-coverage-with-pytest">
<h3>Using coverage with pytest<a class="headerlink" href="#using-coverage-with-pytest" title="Link to this heading"></a></h3>
<p>There is a plug-in for pytest that will run coverage for you when you run your tests:</p>
<p><a class="reference external" href="https://pytest-cov.readthedocs.io/">https://pytest-cov.readthedocs.io/</a></p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="o">..</span> <span class="n">code</span><span class="o">-</span><span class="n">block</span><span class="p">::</span> <span class="n">bash</span>
</pre></div>
</div>
<blockquote>
<div><p>$ pip install pytest-cov</p>
<p># now it can be used
$ pytest –cov code_module test_module.py</p>
</div></blockquote>
<p>or</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>conda<span class="w"> </span>install<span class="w"> </span>pytest-cov
</pre></div>
</div>
<p>There are a number of ways to invoke it and get different reports:</p>
<p>To get a nifty html report:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pytest<span class="w"> </span>--cov<span class="w"> </span>code_module<span class="w"> </span>--cov-report<span class="w"> </span>html<span class="w"> </span>test_module.py
</pre></div>
</div>
<p>Or you can run <code class="docutils literal notranslate"><span class="pre">coverage</span> <span class="pre">html</span></code> after running the tests.</p>
</section>
</section>
<section id="doctests">
<h2>Doctests<a class="headerlink" href="#doctests" title="Link to this heading"></a></h2>
<p>Tests placed in docstrings to demonstrate usage of a component to a
human in a machine testable way</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">square</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
<span class="w"> </span><span class="sd">"""</span>
<span class="sd"> Squares x.</span>
<span class="sd"> >>> square(2)</span>
<span class="sd"> 4</span>
<span class="sd"> >>> square(-2)</span>
<span class="sd"> 4</span>
<span class="sd"> """</span>
<span class="k">return</span> <span class="n">x</span> <span class="o">*</span> <span class="n">x</span>
</pre></div>
</div>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>python<span class="w"> </span>-m<span class="w"> </span>doctest<span class="w"> </span>-v<span class="w"> </span>example.py
</pre></div>
</div>
<p>Now generate documentation, using epydoc for example:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>epydoc<span class="w"> </span>example.py
</pre></div>
</div>
<p><a class="reference external" href="http://docs.python.org/3/library/doctest.html">http://docs.python.org/3/library/doctest.html</a></p>
<p><a class="reference external" href="http://www.python.org/dev/peps/pep-0257/">http://www.python.org/dev/peps/pep-0257/</a></p>
<p><a class="reference external" href="http://epydoc.sourceforge.net/">http://epydoc.sourceforge.net/</a></p>
<p>These days, most Python projects use Sphinx to do their documentation:</p>
<p><a class="reference external" href="http://sphinx-doc.org/">http://sphinx-doc.org/</a></p>
<p>Well worth checking out – and you can have Sphinx run your doctests for you.</p>
<section id="my-take">
<h3>My Take:<a class="headerlink" href="#my-take" title="Link to this heading"></a></h3>
<p>doctests are really cool – but they are more a way to test your documentation, than a way to test your code.
Which is great – you can have examples in your docs, and know that they are still correct.</p>
</section>
</section>
<section id="test-driven-development-tdd">
<h2>Test Driven Development (TDD)<a class="headerlink" href="#test-driven-development-tdd" title="Link to this heading"></a></h2>
<p>In TDD, the tests are written to meet the requirements before the code
exists.</p>
<p>Once the collection of tests passes, the requirement is considered met.</p>
<p>We’ve been trying to get you to do this from the beginning of this class :-)</p>
<p>We don’t always want to run the entire test suite. In order to run a
single test with pytest:</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pytest<span class="w"> </span>-k<span class="w"> </span><span class="s2">"test_divide"</span>
</pre></div>
</div>
<p>The -k means:</p>
<blockquote>
<div><p>only run tests which match the given substring expression. An expression is a python evaluatable expression where all names are substring-matched against test names and their parent classes.</p>
</div></blockquote>
<p>So you can pretty easily select a subset of your tests if they have consistent naming scheme.</p>
</section>
<section id="exercises">
<h2>Exercises<a class="headerlink" href="#exercises" title="Link to this heading"></a></h2>
<ul class="simple">
<li><p>Add unit tests for each method in calculator_functions.py</p></li>
<li><p>Add fixtures via setUp/tearDown methods and setUpClass/tearDownClass
class methods. Are they behaving how you expect?</p></li>
</ul>
<p>or</p>
<ul class="simple">
<li><p>Use pytest fixtures instead.</p></li>
<li><p>Add additional unit tests for floating point calculations</p></li>
<li><p>Fix any failures in the code</p></li>
<li><p>Add doctests to calculator_functions.py</p></li>
</ul>
<p>Here are the files you’ll need:</p>
<p><a class="reference download internal" download="" href="../_downloads/35f2dfffa997e2acb10d5b41052228f0/calculator.py"><code class="xref download docutils literal notranslate"><span class="pre">calculator.py</span></code></a></p>
<p><a class="reference download internal" download="" href="../_downloads/5cdf55a8048eb097483f483519f663d2/calculator_functions.py"><code class="xref download docutils literal notranslate"><span class="pre">calculator_functions.py</span></code></a></p>
<p><a class="reference download internal" download="" href="../_downloads/366509eb0a8e44364a5ebd1270c18204/calculator_test.sh"><code class="xref download docutils literal notranslate"><span class="pre">calculator_test.sh</span></code></a></p>
<p><a class="reference download internal" download="" href="../_downloads/5a5fdff5de7f7f1944ea29e74393d4b9/test_calculator_pytest.py"><code class="xref download docutils literal notranslate"><span class="pre">test_calculator_pytest.py</span></code></a></p>
<p><a class="reference download internal" download="" href="../_downloads/20c2b8dec0016f4a222d42c9fddfe3b2/calculator_test_suite.py"><code class="xref download docutils literal notranslate"><span class="pre">calculator_test_suite.py</span></code></a></p>
<p><a class="reference download internal" download="" href="../_downloads/151bc0d618fe0bd8cac43e0b183fd66f/test_calculator.py"><code class="xref download docutils literal notranslate"><span class="pre">test_calculator.py</span></code></a></p>
</section>
<section id="mocking">
<h2>Mocking<a class="headerlink" href="#mocking" title="Link to this heading"></a></h2>
<section id="now-we-ve-got-the-tools-to-really-test">
<h3>Now we’ve got the tools to really test<a class="headerlink" href="#now-we-ve-got-the-tools-to-really-test" title="Link to this heading"></a></h3>
<p>Consider the application in:</p>
<p><a class="reference download internal" download="" href="../_downloads/5cbd2d019dd7b865fe9e576fc8f42d5b/wikidef.zip"><code class="xref download docutils literal notranslate"><span class="pre">wikidef.zip</span></code></a></p>
<p>Give the command line utility a subject, and it will return a definition.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>./define.py<span class="w"> </span>Robot
</pre></div>
</div>
<p>How can we test our application code without abusing (and waiting for)
Wikipedia?</p>
</section>
<section id="using-mock-objects">
<h3>Using Mock objects<a class="headerlink" href="#using-mock-objects" title="Link to this heading"></a></h3>
<p>Using Mock objects to test an application with service dependencies</p>
<p>Mock objects replace real objects in your code at runtime during test</p>
<p>This allows you to test code which calls these objects without having
their actual code run</p>
<p>Useful for testing objects which depend on unimplemented code, resources
which are expensive, or resources which are unavailable during test
execution</p>
<p><a class="reference external" href="https://docs.python.org/3/library/unittest.mock-examples.html">https://docs.python.org/3/library/unittest.mock-examples.html</a></p>
</section>
<section id="mocks">
<h3>Mocks<a class="headerlink" href="#mocks" title="Link to this heading"></a></h3>
<p>The MagicMock class will keep track of calls to it so we can verify
that the class is being called correctly, without having to execute the
code underneath</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">unittest</span><span class="w"> </span><span class="kn">import</span> <span class="n">mock</span>
<span class="n">mock_object</span> <span class="o">=</span> <span class="n">mock</span><span class="o">.</span><span class="n">MagicMock</span><span class="p">()</span>
<span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s2">"foo return"</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">call_count</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="p">())</span>
<span class="nb">print</span><span class="p">(</span><span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">call_count</span><span class="p">)</span>
<span class="c1"># raise an exception by assigning to the side_effect attribute</span>
<span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="o">.</span><span class="n">side_effect</span> <span class="o">=</span> <span class="ne">Exception</span>
<span class="n">mock_object</span><span class="o">.</span><span class="n">foo</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="easy-mocking-with-mock-patch">
<h3>Easy mocking with mock.patch<a class="headerlink" href="#easy-mocking-with-mock-patch" title="Link to this heading"></a></h3>
<p>patch acts as a function decorator, class decorator, or a context
manager</p>
<p>Inside the body of the function or with statement, the target is patched
with a new object. When the function/with statement exits the patch is
undone</p>
</section>
<section id="using-patch">
<h3>Using patch<a class="headerlink" href="#using-patch" title="Link to this heading"></a></h3>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="c1"># patch with a decorator</span>
<span class="nd">@patch</span><span class="o">.</span><span class="n">object</span><span class="p">(</span><span class="n">Wikipedia</span><span class="p">,</span> <span class="s1">'article'</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_article_success_decorator_mocked</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">mock_method</span><span class="p">):</span>
<span class="n">article</span> <span class="o">=</span> <span class="n">Definitions</span><span class="o">.</span><span class="n">article</span><span class="p">(</span><span class="s2">"Robot"</span><span class="p">)</span>
<span class="n">mock_method</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s2">"Robot"</span><span class="p">)</span>
<span class="c1"># patch with a context manager</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_article_success_context_manager_mocked</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">with</span> <span class="n">patch</span><span class="o">.</span><span class="n">object</span><span class="p">(</span><span class="n">Wikipedia</span><span class="p">,</span> <span class="s1">'article'</span><span class="p">)</span> <span class="k">as</span> <span class="n">mock_method</span><span class="p">:</span>
<span class="n">article</span> <span class="o">=</span> <span class="n">Definitions</span><span class="o">.</span><span class="n">article</span><span class="p">(</span><span class="s2">"Robot"</span><span class="p">)</span>
<span class="n">mock_method</span><span class="o">.</span><span class="n">assert_called_once_with</span><span class="p">(</span><span class="s2">"Robot"</span><span class="p">)</span>
</pre></div>
</div>
<p>There are a number of ways to use <code class="docutils literal notranslate"><span class="pre">mock.patch</span></code> – this is a nice discussion of that: <a class="reference external" href="http://treyhunner.com/2014/10/the-many-flavors-of-mock-dot-patch/">The Many Flavors of mock.patch</a></p>
</section>
<section id="mocking-with-pytest">
<h3>mocking with pytest<a class="headerlink" href="#mocking-with-pytest" title="Link to this heading"></a></h3>
<p>pytest uses the same mock library, but has a little different syntax.</p>
<p>Here is an example of mocking <code class="docutils literal notranslate"><span class="pre">input()</span></code> with pytest:</p>
<p><a class="reference download internal" download="" href="../_downloads/a227f1937bb2aa9586ba90b83f79fc72/test_mock_input.py"><code class="xref download docutils literal notranslate"><span class="pre">test_mock_input.py</span></code></a></p>
<p><code class="docutils literal notranslate"><span class="pre">pytest-mock</span></code> is a utility that makes it easier to mock
with pytest.</p>
<div class="highlight-bash notranslate"><div class="highlight"><pre><span></span>$<span class="w"> </span>pip<span class="w"> </span>install<span class="w"> </span>pytest-mock
</pre></div>
</div>
<p>Here is a nice blog post about using it:</p>
<p><a class="reference external" href="https://medium.com/@bfortuner/python-unit-testing-with-pytest-and-mock-197499c4623c">https://medium.com/@bfortuner/python-unit-testing-with-pytest-and-mock-197499c4623c</a></p>
<section id="exercise">
<h4>Exercise<a class="headerlink" href="#exercise" title="Link to this heading"></a></h4>
<p>When <code class="docutils literal notranslate"><span class="pre">define.py</span></code> is given the name of a non-existent article, an exception
is thrown. This exception causes another exception to occur, and the whole thing is not very readable. Why does this happen?</p>
<p>Use what you know about exceptions to throw a better exception, and
then add a new test that confirms this behavior. Use mock for your test, so you are not hammering Wikipedia.</p>
</section>
</section>
<section id="mocking-a-builtin">
<h3>Mocking a builtin<a class="headerlink" href="#mocking-a-builtin" title="Link to this heading"></a></h3>
<p>Say you would like to mock input in this function in a file called mock_input.py:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">get_input</span><span class="p">():</span>
<span class="n">color</span> <span class="o">=</span> <span class="nb">input</span><span class="p">(</span><span class="s2">"What is your favorite color? "</span><span class="p">)</span>
<span class="k">return</span> <span class="n">color</span>
</pre></div>
</div>
<p>In your test file, you would do this:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="nd">@mock</span><span class="o">.</span><span class="n">patch</span><span class="p">(</span><span class="s1">'builtins.input'</span><span class="p">)</span>
<span class="k">def</span><span class="w"> </span><span class="nf">test_get_input</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">new_mocked_input</span><span class="p">):</span>
<span class="n">new_mocked_input</span><span class="o">.</span><span class="n">return_value</span> <span class="o">=</span> <span class="s1">'blue'</span>
<span class="bp">self</span><span class="o">.</span><span class="n">assertEqual</span><span class="p">(</span><span class="n">mock_input</span><span class="o">.</span><span class="n">get_input</span><span class="p">(),</span> <span class="s1">'blue'</span><span class="p">)</span>
</pre></div>
</div>
</section>
</section>
</section>
</div>
</div>
<footer><div class="rst-footer-buttons" role="navigation" aria-label="Footer">
<a href="TestDrivenDevelopment.html" class="btn btn-neutral float-left" title="Test Driven Development" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left" aria-hidden="true"></span> Previous</a>
<a href="../exercises/unit_testing/unit_testing.html" class="btn btn-neutral float-right" title="Introduction To Unit Testing" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right" aria-hidden="true"></span></a>
</div>
<hr/>
<div role="contentinfo">
<p>© Copyright 2025, Natasha Aleksandrova, Christopher Barker, Brian Dorsey, Cris Ewing, Christy Heaton, Jon Jacky, Maria McKinley, Andy Miles, Rick Riehle, Joseph Schilz, Joseph Sheedy, Hosung Song. Creative Commons Attribution-ShareAlike 4.0 license.</p>
</div>
Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
<a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script>
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>