---
description: Run unit tests on notebooks in parallel
order: 12
output-file: test.html
title: test

---
<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

Press Ctrl-C during a hung test run to see what each notebook is executing. Each worker prints the synchronous stack and the await chains of pending asyncio tasks. An awaited coroutine can be stuck somewhere the synchronous stack doesn't show.

Workers print their diagnostics in separate blocks and exit immediately. The shell must not catch the interrupt and continue running cells. Idle workers exit silently.

---

[source](https://github.com/AnswerDotAI/nbdev/blob/main/nbdev/test.py#L69)

### nb\_test\_cells

```python
def nb_test_cells(
    fn, # Notebook path or `Notebook`
    skip_flags=None, # Flags marking cells to skip
    force_flags=None, # Flags marking cells to always run
):
```

*The cells [`test_nb`](test.md#test_nb) would run in the notebook at `fn`*

[`nb_test_cells`](test.md#nb_test_cells) lists the cells selected for testing. It excludes non-Python notebooks and notebooks with `skip_exec: true` in frontmatter. Within a Python notebook, `eval` directives and skip flags determine which code cells run.

Pass `skip_flags=['notest']` to leave out cells marked `notest`:

```python
_nb = Path('../../tests/directives.ipynb')
cells = nb_test_cells(_nb, skip_flags=['notest'])
assert not any('notest' in c.source for c in cells)
len(cells), len(nb_test_cells(_nb))
```

---

[source](https://github.com/AnswerDotAI/nbdev/blob/main/nbdev/test.py#L85)

### test\_nb

```python
def test_nb(
    fn, # file name of notebook to test
    skip_flags=None, # list of flags marking cells to skip
    force_flags=None, # list of flags marking cells to always run
    do_print:bool=False, # print completion?
    showerr:bool=True, # print errors to stderr?
    basepath=None, # path to add to sys.path
    verbose:bool=False, # stream stdout/stderr from cells to console?
    save:bool=False, # write outputs back to notebook on success?
    use_ipython:bool=False, # Run with real IPython instead of the lightweight shell
    cell_timeout:int=600, # seconds before each cell times out (None: no limit)
    cell_timing_min:float=None, # print cells slower than this many seconds (None: no timing output)
):
```

*Execute tests in notebook in `fn` except those with `skip_flags`*

[`test_nb`](test.md#test_nb) runs cells on `CaptureShell`'s loop thread and waits for each result. The worker's main thread can still handle Ctrl-C while a cell is stuck.

`cell_timeout` limits execution even when synchronous code blocks the loop. A timeout fails the notebook with a `TimeoutError` identifying the cell and stuck tasks.

Failure reports use `sys.__stderr__` to bypass cell output capture. Uncancellable cell code can still be running after a timeout, with its captured `sys.stderr` still installed.

[`test_nb`](test.md#test_nb) returns a success flag and duration. A notebook with `skip_exec: true` skips all execution and reports success. Use this for notebooks that need unavailable credentials or live services.

This test succeeds when we skip the `notest` cell:

```python
_nb = Path('../../tests/directives.ipynb')
success,duration = test_nb(_nb, skip_flags=['notest'])
assert success
```

The `notest` cell raises an exception. Including it makes the test fail:

```python
_nb = Path('../../tests/directives.ipynb')
success,duration = test_nb(_nb, showerr=False)
assert not success
```

Use `cell_timing_min` to report slow cells. Reports give the notebook name, cell ID and elapsed seconds. Try `--cell-timing-min=0.1` to start.

The `nbdev-test` command reads defaults for `cell_timeout` and `cell_timing_min` from configuration. Set them in `~/.config/nbdev/config.toml` or your project's `[tool.nbdev]` table.

Set default skip flags in `tst_flags` under `[tool.nbdev]` in `pyproject.toml`. To run cells with those flags, pass them in `force_flags` to [`test_nb`](test.md#test_nb), or use `--flags` with `nbdev-test`.

Tests use execnb's lightweight shell by default. For a project that tests IPython itself, such as an extension, install `execnb[ipython]` and set `test_use_ipython = true` under `[tool.nbdev]`, or pass `--use-ipython` for one run.

---

[source](https://github.com/AnswerDotAI/nbdev/blob/main/nbdev/test.py#L148)

### nbdev\_test

```python
def nbdev_test(
    path:str=None, # A notebook, directory, or full-path glob to test
    flags:str='', # Space separated list of test flags to run that are normally ignored
    n_workers:int=None, # Number of workers
    timing:bool=False, # Time each notebook to see which are slow
    do_print:bool=False, # Print start and end of each notebook
    pause:float=0.01, # Pause time (in seconds) between notebooks to avoid race conditions
    ignore_fname:str='.notest', # Filename that will result in siblings being ignored
    verbose:bool=False, # Print stdout/stderr from notebook cells?
    save:bool=False, # Write outputs back to notebooks on success?
    cell_timeout:int=None, # Seconds before each cell times out (0: no limit; default `cell_timeout` config, 600)
    cell_timing_min:float=None, # Print cells slower than this many seconds (default `cell_timing_min` config, else none)
    use_ipython:bool=False, # Run with real IPython (also enabled by the test_use_ipython config)
    *, symlinks:bool=False, # Follow symlinks?
    file_glob:str='*.ipynb', # Only include files matching glob
    file_re:str=None, # Only include files matching regex
    path_glob:str=None, # Only include files whose full paths match glob (wildcards match separators)
    path_re:str=None, # Only include files whose full paths match regex
    folder_re:str=None, # Only enter folders matching regex
    skip_file_glob:str=None, # Skip files matching glob
    skip_file_re:str='^[_.]', # Skip files matching regex
    skip_folder_re:str='^[_.]', # Skip folders matching regex
):
```

*Test in parallel notebooks matching `path`, each in a process of its own, passing along `flags`*

Use `test_setup` for preparation shared by several notebooks, such as downloading a dataset. Running this before workers start avoids competing downloads that can exhaust cell timeouts.

Set `test_setup` under `[tool.nbdev]` to a `module:callable`. `nbdev-test` calls it once in the parent process with all cells selected by [`nb_test_cells`](test.md#nb_test_cells). Its output goes directly to the console, where it can report progress.

```python
nbdev_test(n_workers=0)
```

```output
Success.
```

You can even run `nbdev-test` in non nbdev projects, for example, you can test an individual notebook like so:

```
nbdev-test ../../tests/minimal.ipynb --do-print
```

Or you can test an entire directory of notebooks filtered for only those that match a regular expression:

```
nbdev-test ../../tests --file-re '.*test.ipynb' --do-print
```

An omitted path uses the configured `nbs_path`. An existing file or directory is used directly. A path that does not exist is treated as a full-path glob filter over the working directory's tree. Quote patterns to prevent shell expansion:

```sh
nbdev-test 'nbs/api/*test*.ipynb'
```

The glob uses `fnmatch`: `*` matches directory separators and `**` has no special meaning. Use `--path-re` for regex filtering on full paths. `--file-re` matches filenames only.

## Eval

A cell's `eval` directive overrides the notebook default. Set that default in frontmatter or `metadata.nbdev`. Without either, cells run. [`test_nb`](test.md#test_nb) uses `fastcore.nbio.does_cell_eval` for this decision.

For a slow or service-dependent notebook, set notebook-level `eval: false` and mark individual testable cells with `#| eval: true`. This differs from `skip_exec: true`, which skips the entire notebook regardless of cell directives.

Here the marked cell runs. The unmarked cell must not run:

```python
nb = new_nb([mk_cell('---\neval: false\n---', 'raw'), mk_cell('raise Exception("unmarked: must not run")'),
    mk_cell('#| eval: true\nx = 1')])
assert [c.source for c in nb_test_cells(nb)] == ['#| eval: true\nx = 1']
```
