Integration testing#
ploomber-engine
allows you to test output cells from your notebooks.
Example#
Install dependencies:
%pip install ploomber-engine --quiet
Note: you may need to restart the kernel to use updated packages.
Download sample notebook:
%%sh
curl https://raw.githubusercontent.com/ploomber/ploomber-engine/main/examples/nb.ipynb --output testing-demo.ipynb
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
0 1257 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
100 1257 100 1257 0 0 7008 0 --:--:-- --:--:-- --:--:-- 6983
Run the notebook and get the output variables:
from ploomber_engine.ipython import PloomberClient
client = PloomberClient.from_path("testing-demo.ipynb")
namespace = client.get_namespace()
namespace
0%| | 0/4 [00:00<?, ?it/s]
Executing cell: 1: 0%| | 0/4 [00:00<?, ?it/s]
Executing cell: 2: 0%| | 0/4 [00:00<?, ?it/s]
Executing cell: 3: 0%| | 0/4 [00:00<?, ?it/s]
Executing cell: 4: 0%| | 0/4 [00:00<?, ?it/s]
Executing cell: 4: 100%|█████████████████████████| 4/4 [00:00<00:00, 714.84it/s]
{'add': <function __main__.add(x, y)>,
'multiply': <function __main__.multiply(x, y)>,
'a': 42,
'b': 200}
Now we can check if the variables in the notebook have the values we expected:
assert namespace["a"] == 42
assert namespace["b"] == 200