Deploy AI apps for free on Ploomber Cloud!

Integration testing

Contents

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
100  1257  100  1257    0     0   9447      0 --:--:-- --:--:-- --:--:--  9522

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, 596.37it/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