File: index.md

package info (click to toggle)
python-testbook 0.4.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 408 kB
  • sloc: python: 1,045; makefile: 11
file content (66 lines) | stat: -rw-r--r-- 1,576 bytes parent folder | download
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
# Installation and Getting Started

`testbook` is a unit testing framework for testing code in Jupyter Notebooks.

## Installing `testbook`

Using a virtual environment or system Python:

```{code-block} bash
pip install testbook
```

Using Anaconda:

```{code-block} bash
conda install testbook
```

## What is a Jupyter Notebook?

[An introduction to Jupyter](https://jupyter-notebook-beginner-guide.readthedocs.io/en/latest/what_is_jupyter.html)

## Installing and Launching Jupyter Notebook

[How to install Jupyter](https://jupyterlab.readthedocs.io/en/stable/getting_started/installation.html)

```{code-block} bash
jupyter lab
```

## Create your first test

Create a new notebook:

To do add image

Write the following code into the first cell of a Jupyter Notebook:

```{code-block} python
def foo(x):
    return x + 1
```

Save this Notebook as `notebook.ipynb`.

Create a new `.py` file. In this new file, write the following unit test:

```{code-block} python
from testbook import testbook

@testbook('notebook.ipynb', execute=True)
def test_foo(tb):
    foo = tb.get("foo")

    assert foo(2) == 3
```

That's it! You can now execute the test.

## General workflow when using testbook to write a unit test

1. Use `testbook.testbook` as a decorator or context manager to specify the path to the Jupyter Notebook. Passing `execute=True` will execute all the cells, and passing `execute=['cell-tag-1', 'cell-tag-2']` will only execute specific cells identified by cell tags.

2. Obtain references to objects under test using the `.get` method. 

3. Write the test!