Skip to content

Commit

Permalink
add testbook
Browse files Browse the repository at this point in the history
  • Loading branch information
khuyentran1401 committed Jan 29, 2024
1 parent 0ed2591 commit c206001
Show file tree
Hide file tree
Showing 10 changed files with 1,095 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Chapter5/speed_up_code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3517,7 +3517,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
"version": "3.11.6"
},
"toc": {
"base_numbering": 1,
Expand Down
35 changes: 35 additions & 0 deletions Chapter7/example_notebook2.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def add(num1: float, num2: float) -> float:\n",
" return num1 + num2"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Data-science",
"language": "python",
"name": "data-science"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
160 changes: 155 additions & 5 deletions Chapter7/jupyter_notebook.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,15 @@
"id": "5fcc2e07",
"metadata": {},
"source": [
"Imagine the notebook `example_notebook.ipnb` looks like the below:\n",
"Imagine the notebook `example_notebook.ipnb` looks like the below:"
]
},
{
"cell_type": "markdown",
"id": "6817a3fb",
"metadata": {},
"source": [
"```python\n",
"\n",
"import pandas as pd\n",
"\n",
"import numpy as np\n",
Expand All @@ -635,7 +641,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"id": "80fbb690",
"metadata": {
"ExecuteTime": {
Expand All @@ -651,9 +657,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"reformatted example_notebook.ipynb\n",
"All done! ✨ 🍰 ✨\n",
"1 file reformatted.\n"
"1 file left unchanged.\n"
]
}
],
Expand Down Expand Up @@ -1082,6 +1087,151 @@
"source": [
"[Link to jupyter-ai](https://github.com/jupyterlab/jupyter-ai)."
]
},
{
"cell_type": "markdown",
"id": "8b5dc5b4",
"metadata": {},
"source": [
"### testbook: Write Clean Unit Tests for Notebooks"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "cd4d8dd8",
"metadata": {
"tags": [
"hide-cell"
]
},
"outputs": [],
"source": [
"!pip install testbook"
]
},
{
"cell_type": "markdown",
"id": "052ecb8b",
"metadata": {},
"source": [
"Writing unit tests for notebooks within the notebooks themselves can lead to a messy notebook.\n",
"\n",
"testbook allows unit tests to be run against notebooks in separate test files, effectively treating .ipynb files as .py files.\n",
"\n",
"For example, consider the following code cell in a Jupyter Notebook \"example_notebook2.ipynb\":"
]
},
{
"cell_type": "markdown",
"id": "c4f600a9",
"metadata": {},
"source": [
"```python\n",
"# example_notebook2.ipynb\n",
"def add(num1: float, num2: float) -> float:\n",
" return num1 + num2\n",
"```"
]
},
{
"cell_type": "markdown",
"id": "4a55dfd8",
"metadata": {},
"source": [
"With testbook, you can write a unit test using testbook in a Python file \"test_example.py\" as follows:"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "a63779d0",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_example.py\n"
]
}
],
"source": [
"%%writefile test_example.py\n",
"from testbook import testbook\n",
"\n",
"@testbook('example_notebook2.ipynb', execute=True)\n",
"def test_func(tb):\n",
" add = tb.get(\"add\")\n",
" assert add(1, 2) == 3"
]
},
{
"cell_type": "markdown",
"id": "3714e761",
"metadata": {},
"source": [
"Then pytest can be used to run the test:"
]
},
{
"cell_type": "markdown",
"id": "26b60292",
"metadata": {},
"source": [
"```bash\n",
"$ pytest test_example.py\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "fea9e79a",
"metadata": {
"tags": [
"remove-input"
]
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1m============================= test session starts ==============================\u001b[0m\n",
"platform darwin -- Python 3.11.2, pytest-7.4.3, pluggy-1.3.0\n",
"rootdir: /Users/khuyentran/book/Efficient_Python_tricks_and_tools_for_data_scientists/Chapter7\n",
"plugins: dvc-3.28.0, hydra-core-1.3.2, typeguard-4.1.5, anyio-4.2.0, hypothesis-6.88.4\n",
"collected 1 item \u001b[0m\n",
"\n",
"test_example.py \u001b[32m.\u001b[0m\u001b[33m [100%]\u001b[0m\n",
"\n",
"\u001b[33m=============================== warnings summary ===============================\u001b[0m\n",
"../../../.pyenv/versions/3.11.2/lib/python3.11/site-packages/jupyter_client/connect.py:22\n",
" /Users/khuyentran/.pyenv/versions/3.11.2/lib/python3.11/site-packages/jupyter_client/connect.py:22: DeprecationWarning: Jupyter is migrating its paths to use standard platformdirs\n",
" given by the platformdirs library. To remove this warning and\n",
" see the appropriate new directories, set the environment variable\n",
" `JUPYTER_PLATFORM_DIRS=1` and then run `jupyter --paths`.\n",
" The use of platformdirs will be the default in `jupyter_core` v6\n",
" from jupyter_core.paths import jupyter_data_dir, jupyter_runtime_dir, secure_write\n",
"\n",
"-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html\n",
"\u001b[33m========================= \u001b[32m1 passed\u001b[0m, \u001b[33m\u001b[1m1 warning\u001b[0m\u001b[33m in 1.48s\u001b[0m\u001b[33m =========================\u001b[0m\n",
"\u001b[0m"
]
}
],
"source": [
"!pytest test_example.py"
]
},
{
"cell_type": "markdown",
"id": "97de4851",
"metadata": {},
"source": [
"[Link to testbook](https://bit.ly/49dzOJ4)."
]
}
],
"metadata": {
Expand Down

0 comments on commit c206001

Please sign in to comment.