-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
test_ipynb.py
93 lines (75 loc) · 2.53 KB
/
test_ipynb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.2'
# jupytext_version: 1.2.0
# kernelspec:
# display_name: deepbedmap
# language: python
# name: deepbedmap
# ---
# %% [markdown]
# # Behavioural Driven Development Testing for Jupyter Notebooks
#
# Handy way to process the run unit tests (via doctest) and integration tests (via behave) in jupyter notebooks (.ipynb) containing Python functions.
# The script will convert an .ipynb to a string format (basically a .py file), loads them as modules, and runs the tests on them.
# To run it in the console, do:
#
# python -m pytest --verbose --disable-warnings --nbval test_ipynb.ipynb
#
# The script should tell you which ipynb file's doctests has failed (e.g. srgan_train.ipynb).
# You can then open up this very jupyter notebook to debug and inspect the situation further.
# %%
import doctest
import os
import sys
import behave.__main__
from features.environment import _load_ipynb_modules
def _unit_test_ipynb(path: str):
"""
Unit tests on loaded modules from a .ipynb file.
Uses doctest.
"""
assert path.endswith(".ipynb")
module = _load_ipynb_modules(ipynb_path=path)
num_failures, num_attempted = doctest.testmod(m=module, verbose=True)
if num_failures > 0:
sys.exit(num_failures)
def _integration_test_ipynb(path: str, summary: bool = False):
"""
Integration tests on various feature behaviours inside a .feature file.
Uses behave.
"""
assert os.path.exists(path=path)
assert path.endswith(".feature")
if summary is False:
args = f"--tags ~@skip --no-summary {path}"
elif summary is True:
args = f"--tags ~@skip {path}"
num_failures = behave.__main__.main(args=args)
if num_failures > 0:
sys.exit(num_failures)
# %% [markdown]
# ## Unit tests
# Uses [doctest](https://en.wikipedia.org/wiki/Doctest).
# Small tests for each individual function.
# %%
_unit_test_ipynb(path="data_prep.ipynb")
# %%
_unit_test_ipynb(path="srgan_train.ipynb")
# %% [markdown]
# ## Integration tests
#
# Uses [behave](https://github.com/behave/behave).
# Medium sized tests which checks that components work together properly.
# Ensures that the behaviour of features (made up of units) is sound.
# %%
_integration_test_ipynb(path="features/data_prep.feature")
# %%
_integration_test_ipynb(path="features/srgan_train.feature")
# %%
# _integration_test_ipynb(path="features/deepbedmap.feature")