-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
745 lines (633 loc) · 31.4 KB
/
index.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Software Development for Engineering Research - Packaging and Testing</title>
<meta name="description" content="
Lecture on packaging and testing for ME 599: Software Development for Engineering Research
">
<meta name="author" content="Kyle Niemeyer">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<link rel="stylesheet" href="dist/reset.css">
<link rel="stylesheet" href="dist/reveal.css">
<link rel="stylesheet" href="dist/theme/simple.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="plugin/highlight/monokai.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h2>Software Testing and Continuous Integration</h2>
<h3>Software Development for Engineering Research</h3>
<br/>
<h3>Kyle Niemeyer. 11 January 2022</h3>
<h3>ME 599, Corvallis, OR</h3>
</section>
<section>
<section data-markdown>
<textarea data-template>
## Today: **testing** and **continuous integration** of (Python) programs
</textarea>
</section>
<section>
<img src="./images/in_case_of_fire.svg" height="500" alt="In Case of Fire; https://github.com/louim/in-case-of-fire">
<p><small>Source: <a href="https://github.com/louim/in-case-of-fire">github.com/louim/in-case-of-fire</a></small></p>
</section>
<section>
<h2><code>git fire</code></h2>
<p><a href="https://github.com/qw3rtman/git-fire">github.com/qw3rtman/git-fire</a></p>
<p>Creates new branch, adds all files commits, and pushes.</p>
<p>Usage:
<pre><code data-trim data-noescape>
$ git config --global alias.out fire
$ git-fire <message>
$ git out <message>
</code></pre>
</p>
</section>
</section>
<section>
<section>
<h3>Testing your code</h3>
<br/>
<h5 class="fragment">How do you know your code gives the right answers?</h5>
<br/>
<h5 class="fragment">... what about after you make changes?</h5>
<br/>
<h5 class="fragment"><strong>Legacy code:</strong> "code without tests"
(Michael Feathers, <em>Working Effectively with Legacy Code</em>)</h5>
</section>
<section>
<h3>Testing your code</h3>
<br/>
<h5 class="fragment">when to test: <strong>always.</strong></h5>
<br/>
<h5 class="fragment">where to test: <strong>external test suite.</strong></h5>
<br/>
<h5 class="fragment">Example: <code>tests</code> subdirectory inside package.</h5>
<br/>
<p class="fragment"><small>Perfect is the enemy of good; a basic level of tests is better than nothing. But a rigorous test suite will save you time and potential problems in the long run.</small></p>
</section>
<section>
<h3>Why test?</h3>
<br/>
<h5 class="fragment">Testing is a core principle of scientific software; it ensures results are trustworthy.</h5>
<br/>
<h5 class="fragment">Scientific and engineering software is used for planes, power plants, satellites, and decisionmaking.
Thus, correctness of this software is pretty important.
</h5>
<br/>
<p class="fragment"><small>And we all know how easy it is to have mistakes in code without realizing it...</small>
</p>
</section>
<section>
<p class="fragment">
Testing is particularly important if you want contributions to your codebase.
</p>
<ul>
<li class="fragment">
As a contributor, how do I know if my PR hasn’t broken anything?
</li>
<li class="fragment">
As a commiter, how do I know if your PR is ok?
</li>
<li class="fragment">
Unit tests are often a great repository of how different features of a library actually work
</li>
</ul>
</section>
</section>
<section>
<section>
<h3>What and how to test?</h3>
<br/>
<pre><code data-trim class="python fragment">
def kepler_loc(p1, p2, dt, t):
'''Use Kepler's Laws to predict location of celestial body'''
...
return p3
</code></pre>
<br/>
<pre><code data-trim class="python fragment">
def test_kepler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = kepler_loc(p1, p2, 1, 1)
if exp != obs:
raise ValueError("Jupiter is not where it should be!")
</code></pre>
</section>
<section>
<h3>What is a test?</h3>
<br/>
<blockquote>
Tests compare expected outputs versus observed outputs for known inputs.
They do not inspect the body of the function directly.
In fact, the body of a function does not even have to exist for a valid
test to be written.
</blockquote>
<pre><code data-trim class="python fragment">
def test_func():
exp = get_expected()
obs = func(*args, **kwargs)
assert exp == obs
</code></pre>
</section>
<section>
<h3>Good idea: test through assertions</h3>
<p class="fragment">For exactness:</p>
<pre><code data-trim class="python fragment">
def test_kepler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = kepler_loc(p1, p2, 1, 1)
assert exp == obs
</code></pre>
<p class="fragment">For approximate exactness:</p>
<pre><code data-trim class="python fragment">
import numpy as np
def test_kepler_loc():
p1 = jupiter(two_days_ago)
p2 = jupiter(yesterday)
exp = jupiter(today)
obs = kepler_loc(p1, p2, 1, 1)
assert np.allclose(exp, obs)
</code></pre>
</section>
<section>
<h3>Test using <a href="https://docs.pytest.org/en/latest/"><code>pytest</code></a></h3>
<br/>
<pre><code data-trim class="python fragment">
# content of test_sample.py
def inc(x):
return x + 1
def test_answer():
assert inc(3) == 5
</code></pre>
<pre><code data-trim class="bash fragment">
$ pytest
</code></pre>
<p class="fragment"><code>pytest</code> finds all testing modules and functions, and runs them.</p>
</section>
</section>
<section>
<section>
<h3>Kinds of tests</h3><br/>
<p class="fragment"><strong>interior test</strong>: precise points/values do not matter</p>
<p class="fragment"><strong>edge test</strong>: test examines beginning or end of a range</p>
<p class="fragment">Best practice: test all edges and at least one interior point.</p>
</section>
<section>
<p class="fragment">Also <strong>corner cases</strong>: two or more edge cases combined.</p>
<br/>
<pre><code data-trim class="python fragment">
import numpy as np
def sinc2d(x, y):
'''(Describe the function here)'''
if x == 0.0 and y == 0.0:
return 1.0
elif x == 0.0:
return np.sin(y) / y
elif y == 0.0:
return np.sin(x) / x
else:
return (np.sin(x) / x) * (np.sin(y) / y)
</code></pre>
</section>
<section>
<pre><code data-trim class="python">
import numpy as np
from mod import sinc2d
def def test_internal():
exp = (2.0 / np.pi) * (-2.0 / (3.0 * np.pi))
obs = sinc2d(np.pi / 2.0, 3.0 * np.pi / 2.0)
assert np.allclose(exp, obs)
def test_edge_x():
exp = (-2.0 / (3.0 * np.pi))
obs = sinc2d(0.0, 3.0 * np.pi / 2.0)
assert np.allclose(exp, obs)
</code></pre>
<pre><code data-trim class="python">
def test_edge_y():
exp = (2.0 / np.pi)
obs = sinc2d(np.pi / 2.0, 0.0)
assert np.allclose(exp, obs)
def test_corner():
exp = 1.0
obs = sinc2d(0.0, 0.0)
assert np.allclose(exp, obs)
</code></pre>
</section>
<section>
<h3>Test generators</h3><br/>
<pre><code data-trim class="python">
import numpy as np
import pytest
# contents of add.py
def add2(x, y):
return x + y
class Test(object):
@pytest.mark.parametrize('exp, x, y', [
(4, 2, 2),
(5, -5, 10),
(42, 40, 2),
(16, 3, 13),
(-128, 0, -128),
])
def test_add2(self, x, y, exp):
obs = add2(x, y)
assert np.allclose(exp, obs)
</code></pre>
</section>
<section>
<h3>Types of tests</h3><br/>
<ul>
<li class="fragment"><strong>unit test</strong>: interrogate individual functions and methods
</li>
<li class="fragment"><strong>integration test</strong>: verify that multiple pieces of the code work together
</li>
<li class="fragment"><strong>regression test</strong>: confirm that results match prior code results (which are assumed correct)
</li>
</ul>
</section>
<section>
<p>Developers warn against having unit tests without any integration tests</p>
<img width="40%" data-src="./images/unit-tests-dryer.gif">
<img width="40%" data-src="./images/unit-tests-doors.gif">
<small>
Sources: <a href="https://twitter.com/thepracticaldev/status/845638950517706752">twitter/@ThePracticalDev</a>,
<a href="https://twitter.com/withzombies/status/829716565834752000">twitter/@withzombies</a>
</small>
</section>
</section>
<section>
<section>
<h3>Test-Driven Development (TDD)</h3><br/>
<p class="fragment">Write the tests first.</p>
<p class="fragment">Before you write any lines of a function, first write the test for that function.</p>
</section>
<section>
<pre><code data-trim class="python">
from numpy import allclose
from mod import std
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert np.allclose(exp, obs)
</code></pre>
<pre><code data-trim class="python fragment">
def std(vals):
# this must be cheating.
return 1.0
</code></pre>
</section>
<section>
<pre><code data-trim class="python">
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert np.allclose(exp, obs)
def test_std2():
obs = std()
exp = 0.0
assert np.allclose(exp, obs)
def test_std3():
obs = std([0.0, 4.0])
exp = 2.0
assert np.allclose(exp, obs)
</code></pre>
<pre><code data-trim class="python fragment">
def std(vals):
# a bit better, but still not quite generic
if len(vals) == 0:
return 0.0
return vals[-1] / 2.0
</code></pre>
</section>
<section>
<div style="font-size:20px">
<pre><code data-trim class="python">
def test_std1():
obs = std([0.0, 2.0])
exp = 1.0
assert np.allclose(exp, obs)
def test_std2():
obs = std()
exp = 0.0
assert np.allclose(exp, obs)
def test_std3():
obs = std([0.0, 4.0])
exp = 2.0
assert np.allclose(exp, obs)
def test_std4():
obs = std([1.0, 3.0])
exp = 1.0
assert np.allclose(exp, obs)
def test_std4():
obs = std([1.0, 1.0, 1.0])
exp = 0.0
assert np.allclose(exp, obs)
</code></pre>
<pre><code data-trim class="python fragment">
def std(vals):
# finally some math
n = len(vals)
if n == 0:
reutnr 0.0
mu = sum(vals) / n
var = 0.0
for val in vals:
var = var + (val - mu)**2
return (var / n)**0.5
</code></pre>
</div>
</section>
</section>
<section>
<section>
<h2>Test coverage</h2>
<br/>
<p class="fragment">Meaning: percentage of code for which a test exists,
determined by number of line executed during tests
</p>
</section>
<section>
<h2><a href="https://pytest-cov.readthedocs.io"><code>pytest-cov</code></a></h2>
<br/>
<p class="fragment">Instructions:</p>
<ol>
<li class="fragment">
Install <code>pytest-cov</code> using <code>pip</code>/<code>conda</code>
</li>
<li class="fragment">
<code>pytest -vv --cov=./</code>
</li>
<li class="fragment">
Look at coverage; are you at or near 100%?
</li>
<li class="fragment">
Get more detailed information by having it create a report:
<code>pytest -vv --cov=./ --cov-report html</code>
</li>
</ol>
</section>
<section>
<h2>Example</h2>
<br/>
<pre><code data-trim class="fragment python">
# content of test_sample.py
def inc(x):
if x < 0:
return x - 1
return x + 1
def test_answer():
assert inc(3) == 4
</code></pre>
<pre><code data-trim class="fragment bash">
$ pytest -vv test_sample.py --cov=./
main.py::test_answer PASSED
---------- coverage: platform darwin, python 3.5.4-final-0 -----------
Name Stmts Miss Cover
-----------------------------
main.py 6 1 83%
</code></pre>
</section>
<section>
<h2>Coverage report</h2>
<br/>
<img src="./images/coverage-report.png" height="400" alt="Example HTML coverage report from pytest-cov">
</section>
<section>
<pre><code data-trim class="fragment python">
# content of test_sample.py
def inc(x):
if x < 0:
return x - 1
return x + 1
def test_answer():
assert inc(3) == 4
def test_answer_negative():
assert inc(-2) == -3
</code></pre>
<pre><code data-trim class="fragment bash">
$ pytest -vv test_sample.py --cov=./
main.py::test_answer PASSED
main.py::test_answer_negative PASSED
---------- coverage: platform darwin, python 3.5.4-final-0 -----------
Name Stmts Miss Cover
-----------------------------
main.py 8 0 100%
</code></pre>
</section>
<section>
<h3>Coverage overview: work towards 100%</h3>
<br/>
<h3 class="fragment">Use coverage to help you identify missing edge/corner cases</h3>
</section>
</section>
<section>
<section>
<h2>Next: continuous integration</h2>
<br/>
<p class="fragment">Meaning: ensure all changes to your project pass tests through automated
test & build process
</p>
</section>
<section>
<h2>Importance of testing + continuous integration</h2>
<img class="plain" width="60%" src="./images/chemical-shifts-paper.png"/>
<small>
<a href="https://doi.org/10.1021/acs.orglett.9b03216"><code>https://doi.org/10.1021/acs.orglett.9b03216</code></a>
</small>
</section>
<section>
<p class="fragment">
Recent paper found differences in calculated nuclear magnetic resonance chemical shifts on different operating systems
</p>
<p class="fragment">
Using Python scripts published in Nature Protocols in 2014, cited over 130 times
</p>
<p class="fragment">
Error due to differences in file sorting across platforms…
</p>
</section>
<section>
<h3>Getting started with <a href="https://travis-ci.org">Travis CI</a>:</h3>
<br/>
<ol>
<li class="fragment">
Sign in at <a href="https://travis-ci.org">travis-ci.org</a> with your GitHub account
</li>
<li class="fragment">
Go to your profile and enable the repository for the software you want to build & test.
</li>
<li class="fragment">
Add a <code>.travis.yml</code> file to the repository
</li>
<li class="fragment">
Add info to configuration file telling Travis CI what to do:
what programming language (<code>language: python</code>), what it needs to build your
package (<code>install</code>), and how to run the tests (<code>language: script</code>)
</li>
</ol>
</section>
<section>
<h3>Using pip</h3>
<br/>
<pre><code data-trim class="python">
language: python
python:
- "3.5"
- "3.6"
install:
- pip install -r requirements.txt
script:
- pytest -vv --cov=./;
</code></pre>
</section>
<section>
<h4>Contents of <code>requirements.txt</code></h4>
<pre><code data-trim>
#
####### requirements.txt #######
#
###### Requirements without Version Specifiers ######
pytest
pytest-cov
#
###### Requirements with Version Specifiers ######
# See https://www.python.org/dev/peps/pep-0440/#version-specifiers
numpy >= 1.12.0
pint >= 0.7.2
#
</code></pre>
</section>
<section>
<h3>Using conda</h3>
<br/>
<div style="font-size:30px">
<pre class="stretch"><code data-trim class="bash">
language: python
python:
- "3.5"
- "3.6"
install:
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda.sh;
elif [[ "$TRAVIS_OS_NAME" == "osx" ]]; then
wget https://repo.continuum.io/miniconda/Miniconda3-latest-MacOSX-x86_64.sh -O miniconda.sh;
fi
- bash miniconda.sh -b -p $HOME/miniconda
- rm miniconda.sh
- source $HOME/miniconda/etc/profile.d/conda.sh && conda activate
- conda config --set always_yes yes --set changeps1 no
- conda update -q conda
- conda update -q --all
- conda config --append channels conda-forge
- conda env create -qf test-environment.yaml;
- source activate test-environment;
script:
- pytest -vv --cov=./;
</code></pre>
</div>
</section>
<section>
<h4>Contents of <code>test-environment.yaml</code></h4>
<pre><code data-trim class="yaml">
name: test-environment
channels:
- defaults
- conda-forge
dependencies:
- python=3.5
- numpy>=1.12.0
- pytest>=3.2.0
- pytest-cov
- pint>=0.7.2
</code></pre>
</section>
<section>
<h3>Getting started with <a href="https://travis-ci.org">Travis CI</a> (contd.)</h3>
<br/>
<ol start="5">
<li class="fragment">
<code>git add .travis.yml test-environment.yaml</code>, commit, and push to GitHub
</li>
<li class="fragment">
Check the <a href="https://travis-ci.org/auth">build status page</a> to see if the build passes or fails
</li>
<li class="fragment">
(Optional) Add a status badge to your README:
<a href="https://travis-ci.org/travis-ci/travis-web"><img src="https://travis-ci.org/travis-ci/travis-web.svg?branch=master" alt="Build Status"></a>
(Get this from your Repository page on Travis CI)
</li>
</ol>
<br/>
<p class="fragment">GitHub will use Travis CI to tell you if Pull Requests are safe to merge!</p>
</section>
</section>
<section>
<h3>Initial project work</h3>
<br/>
<ol style="font-size:30px">
<li class="fragment">Come up with a name for your project's package (if you haven't already)
</li>
<li class="fragment">Create a repository in the
<a href="https://github.com/SoftwareDevEngResearch">SoftwareDevEngResearch</a> organization.
Initialize with a <code>README</code>, <code>LICENSE</code>, and <code>.gitignore</code>
</li>
<li class="fragment">Fork this repo to your account, and clone to your computer.
</li>
<li class="fragment">Add the basic elements of your package: create a directory with the name,
and inside that create <code>__init__.py</code>.
</li>
<li class="fragment">What modules do you anticipate needing/creating? Let's spend a few minutes
brainstorming how to structure your program.
</li>
<li class="fragment">Commit and push your work, then submit a pull request to the upstream fork.
</li>
</ol>
</section>
<!--<section>
<h3>For next time: start writing your first module</h3><br/>
<p>At minimum, create three functions.</p>
<p>Also create the tests, and make sure they pass!</p>
<p>By Monday, submit your changes as Pull Requests to the upstream fork; I will assign partners to review your PRs.</p>
</section>-->
</div>
</div>
<script src="dist/reveal.js"></script>
<script src="plugin/notes/notes.js"></script>
<script src="plugin/markdown/markdown.js"></script>
<script src="plugin/highlight/highlight.js"></script>
<script>
// More info about initialization & config:
// - https://revealjs.com/initialization/
// - https://revealjs.com/config/
Reveal.initialize({
hash: true,
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealMarkdown, RevealHighlight, RevealNotes ]
});
</script>
</body>
</html>