Skip to content

Commit 04b04c4

Browse files
author
Tania Allard
committed
Add nbval
1 parent 23f123f commit 04b04c4

2 files changed

Lines changed: 70 additions & 2 deletions

File tree

04_Testing.ipynb

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"metadata": {},
2525
"source": [
2626
"There are various approaches to tests software:\n",
27-
"- Assertions\n",
27+
"- Assertions: 🦄 == 🦄\n",
2828
"- Exceptions: within the code serve as ⚠️\n",
2929
"- Unit tests: investigate the behaviour of units of code (e.g functions)\n",
3030
"- Regression tests: defends against 🐛\n",
@@ -179,7 +179,75 @@
179179
"we did *unit testing*!\n",
180180
"Notice something in the functions we just wrote? \n",
181181
"- Set-up: `mean = country.get_mean(interim_data)`\n",
182-
"- Assertions: `assert mean_price == 20.786`"
182+
"- Assertions: `assert mean_price == 20.786`\n",
183+
"\n",
184+
"Now don't forget to commit your code:\n",
185+
"```\n",
186+
"$ git add .\n",
187+
"$ git commit -m \"Add unit test suite\"\n",
188+
"```"
189+
]
190+
},
191+
{
192+
"cell_type": "markdown",
193+
"metadata": {},
194+
"source": [
195+
"# Past as Truth\n",
196+
"\n",
197+
"Regression tests assume that the past is “correct.” They are great for letting developers know when and how a code base has changed. They are not great for letting anyone know why the change occurred. The change between what a code produces now and what it computed before is called a regression.\n",
198+
"\n",
199+
"** How many times have you tried to run a script or a notebook you found online just to realize it is broken?**\n",
200+
"\n",
201+
"Let's do some regression testing on the Jupyter notebook using *nbval*"
202+
]
203+
},
204+
{
205+
"cell_type": "markdown",
206+
"metadata": {},
207+
"source": [
208+
"# nbval\n",
209+
"\n",
210+
"We first need to understand how a Jupyter notebook works. \n",
211+
"All the data is stored in a .json like format (organised key, data values)... this includes the results, code, and markdown.\n",
212+
"\n",
213+
"![json](assets/json.jpg)\n",
214+
"\n",
215+
"Nbval checks the stored values while doing a *mock run* on the notebook and compares the saved version of the notebook vs the results obtained from the mock run \n"
216+
]
217+
},
218+
{
219+
"cell_type": "markdown",
220+
"metadata": {},
221+
"source": [
222+
"Try it on your shell \n",
223+
"\n",
224+
"```\n",
225+
"$ pytest --nbval src/data/00_explore-data.ipynb\n",
226+
"```\n",
227+
"\n",
228+
"What would happen if you were to have a cell like this one?\n",
229+
"```python\n",
230+
"import time\n",
231+
"print('This notebook was last run on: ' + time.strftime('%d/%m/%y') + ' at: ' + time.strftime('%H:%M:%S'))\n",
232+
"```"
233+
]
234+
},
235+
{
236+
"cell_type": "markdown",
237+
"metadata": {},
238+
"source": [
239+
"# Provenance\n",
240+
"\n",
241+
"Image you created a beautiful graph and some results that makes your research Nobel worthy. Of course you ran the workflow multiple times doing minimal changes every single time. But now, 6 months later you need that **one** plot for you Nobel!!\n",
242+
"\n",
243+
"We can use the package [recipy](https://github.com/recipy/recipy) to log each run of your code to a database, keeping track of the input files, output files and the version of your code, and then let you query this database to find out how you actually did create graph.png"
244+
]
245+
},
246+
{
247+
"cell_type": "markdown",
248+
"metadata": {},
249+
"source": [
250+
"Make sure everything is commited to git before carrying on.\n"
183251
]
184252
},
185253
{

assets/json.jpg

9.71 KB
Loading

0 commit comments

Comments
 (0)