|
12 | 12 | "\n", |
13 | 13 | "We now have a fully automated script! 🎉👏🏻🦄\n", |
14 | 14 | "\n", |
15 | | - "The next step is to include **tests**... in fact testing should be a core part of our development process. In fact all of our **reproducible workflows** are analogous to experimental design in the scientific world\n", |
| 15 | + "😕 Such a shame we still cannot guarantee the results are correct... or that there are no bugs.\n", |
16 | 16 | "\n", |
| 17 | + "The next step is to include **tests**... in fact testing should be a core part of our development process. In fact all of our **reproducible workflows** are analogous to experimental design in the scientific world" |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "markdown", |
| 22 | + "metadata": { |
| 23 | + "slideshow": { |
| 24 | + "slide_type": "subslide" |
| 25 | + } |
| 26 | + }, |
| 27 | + "source": [ |
17 | 28 | "\n", |
18 | 29 | "\n", |
19 | 30 | "<small> https://xkcd.com/242/ </small>" |
20 | 31 | ] |
21 | 32 | }, |
22 | 33 | { |
23 | 34 | "cell_type": "markdown", |
24 | | - "metadata": {}, |
| 35 | + "metadata": { |
| 36 | + "slideshow": { |
| 37 | + "slide_type": "subslide" |
| 38 | + } |
| 39 | + }, |
25 | 40 | "source": [ |
26 | | - "There are various approaches to tests software:\n", |
| 41 | + "There are various approaches to test software:\n", |
27 | 42 | "- **Assertions**: 🦄 == 🦄\n", |
28 | | - "- **Exceptions**: within the code serve as warnings ⚠️\n", |
| 43 | + "- **Exceptions**: (within the code) serve as warnings ⚠️\n", |
29 | 44 | "- **Unit tests**: investigate the behaviour of units of code (e.g functions)\n", |
30 | 45 | "- **Regression tests**: defends against 🐛\n", |
31 | 46 | "- **Integration tests**: ⚙️ checks that the pieces work together as expected" |
32 | 47 | ] |
33 | 48 | }, |
34 | 49 | { |
35 | 50 | "cell_type": "markdown", |
36 | | - "metadata": {}, |
| 51 | + "metadata": { |
| 52 | + "slideshow": { |
| 53 | + "slide_type": "subslide" |
| 54 | + } |
| 55 | + }, |
37 | 56 | "source": [ |
38 | 57 | "## Exceptions \n", |
39 | 58 | "Remember when you tried to run `02_visualize-wines.py`? It woud not work unless you had created a figures directory beforehand.\n", |
40 | 59 | "\n", |
41 | 60 | "We can catch this kinds of errors by adding this piece of code:\n", |
42 | 61 | "```python\n", |
43 | 62 | "try:\n", |
| 63 | + " # try to save the figure\n", |
44 | 64 | " fig.savefig(fname, bbox_inches = 'tight')\n", |
45 | 65 | " except OSError as e:\n", |
| 66 | + " # wowza! the directory does not exist\n", |
46 | 67 | " os.makedirs('figures')\n", |
47 | 68 | " print('Creating figures directory')\n", |
48 | 69 | " fig.savefig(fname, bbox_inches='tight')\n", |
49 | 70 | "```\n", |
50 | | - "Now our `runall` should work!!! 🎉🎉" |
| 71 | + "Now our `runall` should work!!! 🎉🎉\n", |
| 72 | + "\n", |
| 73 | + "```\n", |
| 74 | + "$ python src.runall-wine-analysis\n", |
| 75 | + "```" |
51 | 76 | ] |
52 | 77 | }, |
53 | 78 | { |
54 | 79 | "cell_type": "markdown", |
55 | | - "metadata": {}, |
| 80 | + "metadata": { |
| 81 | + "slideshow": { |
| 82 | + "slide_type": "subslide" |
| 83 | + } |
| 84 | + }, |
56 | 85 | "source": [ |
57 | 86 | "## Unit testing\n", |
58 | 87 | "Open `03_country-subset.py` and add the following function:\n", |
|
64 | 93 | " wine = pd.read_csv(filename)\n", |
65 | 94 | " mean_price = wine['price'].mean()\n", |
66 | 95 | " return round(mean_price, 4)\n", |
67 | | - "```\n", |
68 | | - "\n", |
| 96 | + "```" |
| 97 | + ] |
| 98 | + }, |
| 99 | + { |
| 100 | + "cell_type": "markdown", |
| 101 | + "metadata": { |
| 102 | + "slideshow": { |
| 103 | + "slide_type": "subslide" |
| 104 | + } |
| 105 | + }, |
| 106 | + "source": [ |
69 | 107 | "And we will modify this function too:\n", |
70 | 108 | "```python\n", |
71 | 109 | "def get_country(filename, country):\n", |
|
89 | 127 | }, |
90 | 128 | { |
91 | 129 | "cell_type": "markdown", |
92 | | - "metadata": {}, |
| 130 | + "metadata": { |
| 131 | + "slideshow": { |
| 132 | + "slide_type": "subslide" |
| 133 | + } |
| 134 | + }, |
93 | 135 | "source": [ |
94 | 136 | "Now we are going to create our testing suite. \n", |
95 | 137 | "To run the tests we are going to use **pytest**.\n", |
|
108 | 150 | }, |
109 | 151 | { |
110 | 152 | "cell_type": "markdown", |
111 | | - "metadata": {}, |
| 153 | + "metadata": { |
| 154 | + "slideshow": { |
| 155 | + "slide_type": "subslide" |
| 156 | + } |
| 157 | + }, |
112 | 158 | "source": [ |
113 | 159 | "Your test script should look like this:\n", |
114 | 160 | "``` python\n", |
|
132 | 178 | }, |
133 | 179 | { |
134 | 180 | "cell_type": "markdown", |
135 | | - "metadata": {}, |
| 181 | + "metadata": { |
| 182 | + "slideshow": { |
| 183 | + "slide_type": "subslide" |
| 184 | + } |
| 185 | + }, |
136 | 186 | "source": [ |
137 | 187 | "### What if you want all the decimal numbers?\n", |
138 | 188 | "\n", |
|
156 | 206 | }, |
157 | 207 | { |
158 | 208 | "cell_type": "markdown", |
159 | | - "metadata": {}, |
| 209 | + "metadata": { |
| 210 | + "slideshow": { |
| 211 | + "slide_type": "subslide" |
| 212 | + } |
| 213 | + }, |
160 | 214 | "source": [ |
161 | 215 | "### What else could go wrong?\n", |
162 | 216 | "\n", |
|
188 | 242 | }, |
189 | 243 | { |
190 | 244 | "cell_type": "markdown", |
191 | | - "metadata": {}, |
| 245 | + "metadata": { |
| 246 | + "slideshow": { |
| 247 | + "slide_type": "subslide" |
| 248 | + } |
| 249 | + }, |
192 | 250 | "source": [ |
193 | 251 | "Pytest tells us which tests passed and which did not:\n", |
194 | 252 | "\n", |
|
211 | 269 | }, |
212 | 270 | { |
213 | 271 | "cell_type": "markdown", |
214 | | - "metadata": {}, |
| 272 | + "metadata": { |
| 273 | + "slideshow": { |
| 274 | + "slide_type": "subslide" |
| 275 | + } |
| 276 | + }, |
215 | 277 | "source": [ |
216 | 278 | "We now know what kind of bugs we can encounter.\n", |
217 | 279 | "Let's fix this, open `03_subset-country.py` and add the following lines\n", |
|
239 | 301 | }, |
240 | 302 | { |
241 | 303 | "cell_type": "markdown", |
242 | | - "metadata": {}, |
| 304 | + "metadata": { |
| 305 | + "slideshow": { |
| 306 | + "slide_type": "subslide" |
| 307 | + } |
| 308 | + }, |
243 | 309 | "source": [ |
244 | 310 | "### See what we did in the previous steps?\n", |
245 | 311 | "\n", |
|
258 | 324 | }, |
259 | 325 | { |
260 | 326 | "cell_type": "markdown", |
261 | | - "metadata": {}, |
| 327 | + "metadata": { |
| 328 | + "slideshow": { |
| 329 | + "slide_type": "subslide" |
| 330 | + } |
| 331 | + }, |
262 | 332 | "source": [ |
263 | 333 | "## Past as Truth (regression tests)\n", |
264 | 334 | "\n", |
|
271 | 341 | }, |
272 | 342 | { |
273 | 343 | "cell_type": "markdown", |
274 | | - "metadata": {}, |
| 344 | + "metadata": { |
| 345 | + "slideshow": { |
| 346 | + "slide_type": "subslide" |
| 347 | + } |
| 348 | + }, |
275 | 349 | "source": [ |
276 | 350 | "# nbval\n", |
277 | 351 | "\n", |
|
285 | 359 | }, |
286 | 360 | { |
287 | 361 | "cell_type": "markdown", |
288 | | - "metadata": {}, |
| 362 | + "metadata": { |
| 363 | + "slideshow": { |
| 364 | + "slide_type": "subslide" |
| 365 | + } |
| 366 | + }, |
289 | 367 | "source": [ |
290 | 368 | "Try it on your shell \n", |
291 | 369 | "\n", |
|
302 | 380 | }, |
303 | 381 | { |
304 | 382 | "cell_type": "markdown", |
305 | | - "metadata": {}, |
| 383 | + "metadata": { |
| 384 | + "slideshow": { |
| 385 | + "slide_type": "slide" |
| 386 | + } |
| 387 | + }, |
306 | 388 | "source": [ |
307 | 389 | "# Provenance\n", |
308 | 390 | "\n", |
|
313 | 395 | }, |
314 | 396 | { |
315 | 397 | "cell_type": "markdown", |
316 | | - "metadata": {}, |
| 398 | + "metadata": { |
| 399 | + "slideshow": { |
| 400 | + "slide_type": "subslide" |
| 401 | + } |
| 402 | + }, |
317 | 403 | "source": [ |
318 | 404 | "<div class='warn'>Make sure everything is commited to git before carrying on.</div>\n", |
319 | 405 | "<br>\n", |
|
327 | 413 | }, |
328 | 414 | { |
329 | 415 | "cell_type": "markdown", |
330 | | - "metadata": {}, |
| 416 | + "metadata": { |
| 417 | + "slideshow": { |
| 418 | + "slide_type": "subslide" |
| 419 | + } |
| 420 | + }, |
331 | 421 | "source": [ |
332 | 422 | "You can now track the provenance of your project. \n", |
333 | 423 | "\n", |
|
0 commit comments