Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tonyfast/pytest-notebook-import

An example directory structure for testing notebooks with pytest coverage, benchmarking, and hypothesis using importnb.
https://github.com/tonyfast/pytest-notebook-import

Last synced: 6 days ago
JSON representation

An example directory structure for testing notebooks with pytest coverage, benchmarking, and hypothesis using importnb.

Awesome Lists containing this project

README

        

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Binder](https://mybinder.org/badge.svg)](https://mybinder.org/v2/gh/tonyfast/pytest-notebook-import/master?urlpath=lab/tree/readme.ipynb)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Import pytest plugins for hypothesis, coverage, and benchmarking."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from IPython import get_ipython"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting requirements.txt\n"
]
}
],
"source": [
"%%file requirements.txt\n",
"importnb\n",
"pytest\n",
"hypothesis\n",
"pytest-cov\n",
"pytest-benchmark"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can even create test fixtures in a notebook and load them into `conftest.py`"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting conftest.py\n"
]
}
],
"source": [
"%%file conftest.py\n",
"from importnb import Notebook\n",
"with Notebook(): \n",
" from fixtures import *"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwriting test_py.py\n"
]
}
],
"source": [
"%%file test_py.py\n",
"def test_py(): assert True"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"def test_something_in_readme():\n",
" assert True"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Use the pytest api."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"=============================== test session starts ===============================\n",
"platform darwin -- Python 3.6.3, pytest-3.5.1, py-1.5.3, pluggy-0.6.0\n",
"benchmark: 3.1.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)\n",
"rootdir: /Users/tonyfast/_test, inifile:\n",
"plugins: cov-2.5.1, benchmark-3.1.1, hypothesis-3.56.5, importnb-0.2.1\n",
"collected 6 items\n",
"\n",
"readme.ipynb . [ 16%]\n",
"test_notebook.ipynb ... [ 66%]\n",
"test_py.py ..\n",
"\n",
"\n",
"----------------------------------------------- benchmark: 1 tests -----------------------------------------------\n",
"Name (time in ms) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations\n",
"------------------------------------------------------------------------------------------------------------------\n",
"test_sleep 200.0932 201.0421 200.4959 0.3521 200.3819 0.3939 2;0 4.9876 5 1\n",
"------------------------------------------------------------------------------------------------------------------\n",
"\n",
"Legend:\n",
" Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.\n",
" OPS: Operations Per Second, computed as 1 / Mean\n",
"============================ 6 passed in 2.78 seconds =============================\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" __import__('pytest').main(args=[]) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"User ipython to test files with magics."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b]0;IPython: tonyfast/_test\u0007\u001b[1m============================= test session starts ==============================\u001b[0m\n",
"platform darwin -- Python 3.6.3, pytest-3.5.1, py-1.5.3, pluggy-0.6.0\n",
"benchmark: 3.1.1 (defaults: timer=time.perf_counter disable_gc=False min_rounds=5 min_time=0.000005 max_time=1.0 calibration_precision=10 warmup=False warmup_iterations=100000)\n",
"rootdir: /Users/tonyfast/_test, inifile:\n",
"plugins: cov-2.5.1, benchmark-3.1.1, hypothesis-3.56.5, importnb-0.2.1\n",
"collected 6 items \u001b[0m\n",
"\n",
"readme.ipynb .\u001b[36m [ 16%]\u001b[0m\n",
"test_notebook.ipynb ...\u001b[36m [ 66%]\u001b[0m\n",
"test_py.py ..\n",
"\n",
"\n",
"\u001b[33m----------------------------------------------- benchmark: 1 tests -----------------------------------------------\u001b[0m\n",
"Name (time in ms) Min Max Mean StdDev Median IQR Outliers OPS Rounds Iterations\n",
"\u001b[33m------------------------------------------------------------------------------------------------------------------\u001b[0m\n",
"test_sleep \u001b[1m 200.4467\u001b[0m\u001b[1m 204.8579\u001b[0m\u001b[1m 203.1205\u001b[0m\u001b[1m 1.9855\u001b[0m\u001b[1m 203.8270\u001b[0m\u001b[1m 3.4923\u001b[0m 1;0\u001b[1m 4.9232\u001b[0m 5 1\n",
"\u001b[33m------------------------------------------------------------------------------------------------------------------\u001b[0m\n",
"\n",
"Legend:\n",
" Outliers: 1 Standard Deviation from Mean; 1.5 IQR (InterQuartile Range) from 1st Quartile and 3rd Quartile.\n",
" OPS: Operations Per Second, computed as 1 / Mean\n",
"\u001b[32m\u001b[1m=========================== 6 passed in 2.68 seconds ===========================\u001b[0m\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" !ipython -m pytest"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Make the readme."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] Converting notebook readme.ipynb to markdown\n",
"[NbConvertApp] Writing 3578 bytes to readme.md\n"
]
}
],
"source": [
"if __name__ == '__main__':\n",
" !jupyter nbconvert --to markdown readme.ipynb"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "p6",
"language": "python",
"name": "other-env"
},
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}