|
347 | 347 | }
|
348 | 348 | },
|
349 | 349 | "source": [
|
350 |
| - "# nbval\n", |
| 350 | + "## Testing Jupyter notebooks with nbval\n", |
351 | 351 | "\n",
|
352 | 352 | "We first need to understand how a Jupyter notebook works. \n",
|
353 | 353 | "All the data is stored in a .json like format (organised key, data values)... this includes the results, code, and markdown.\n",
|
|
378 | 378 | "```"
|
379 | 379 | ]
|
380 | 380 | },
|
| 381 | + { |
| 382 | + "cell_type": "markdown", |
| 383 | + "metadata": {}, |
| 384 | + "source": [ |
| 385 | + "Since we have a timestamp in the notebook (`time.strftime('%H:%M:%S'))`) and the test is performed at a different time, the tests will fail. \n", |
| 386 | + "\n", |
| 387 | + "We can avoid this by providing a sanitizing file `sanitize.cfg`:\n", |
| 388 | + "<pre><code>\n", |
| 389 | + "[regex1]\n", |
| 390 | + "regex: \\d{1,2}/\\d{1,2}/\\d{2,4}\n", |
| 391 | + "replace: DATE-STAMP\n", |
| 392 | + "[regex2]\n", |
| 393 | + "regex: \\d{2}:\\d{2}:\\d{2}\n", |
| 394 | + "replace: TIME-STAMP\n", |
| 395 | + "</pre></code>\n", |
| 396 | + "\n", |
| 397 | + "This will identify time and data stamp like data in your notebooks. Then you can re run the test using:\n", |
| 398 | + "<pre><code>\n", |
| 399 | + "py.test --nbval demo.ipynb --sanitize-with sanitize_nb.cfg\n", |
| 400 | + "</pre></code>\n", |
| 401 | + "\n", |
| 402 | + "If you are only interested in verifying if your notebooks <strong>are broken or not</strong> (check for exceptions)\n", |
| 403 | + "you can use `--nbval-lax` which runs notebooks and checks for errors, but only compares the outputs of cells with a `#NBVAL_CHECK_OUTPUT` marker comment.\n", |
| 404 | + "<pre><code>\n", |
| 405 | + "$ py.test --nbval-lax classify-demo.ipynb\n", |
| 406 | + "</pre></code>\n", |
| 407 | + "\n" |
| 408 | + ] |
| 409 | + }, |
381 | 410 | {
|
382 | 411 | "cell_type": "markdown",
|
383 | 412 | "metadata": {},
|
|
567 | 596 | ],
|
568 | 597 | "metadata": {
|
569 | 598 | "kernelspec": {
|
570 |
| - "display_name": "Python 3", |
| 599 | + "display_name": "Python [default]", |
571 | 600 | "language": "python",
|
572 | 601 | "name": "python3"
|
573 | 602 | },
|
|
0 commit comments