Reference

This part of the documentation lists the full API reference of all public classes and functions.

testbook.client module

class testbook.client.TestbookNotebookClient(nb, km=None, **kw)

Bases: nbclient.client.NotebookClient

cell_execute_result(cell: Union[int, str]) → List[Dict[str, Any]]

Return the execute results of cell at a given index or with a given tag.

Each result is expressed with a dictionary for which the key is the mimetype of the data. A same result can have different representation corresponding to different mimetype.

Parameters

cell (int or str) – The index or tag to look for

Returns

The execute results

Return type

List[Dict[str, Any]]

Raises
cell_output_text(cell) → str

Return cell text output

property cells
execute() → None

Executes all cells

execute_cell(cell, **kwargs) → Union[Dict, List[Dict]]

Executes a cell or list of cells

get(item)
inject(code: str, args: List = None, kwargs: Dict = None, run: bool = True, before: Union[str, int, None] = None, after: Union[str, int, None] = None, pop: bool = False) → testbook.testbooknode.TestbookNode

Injects and executes given code block

Parameters
  • code (str) – Code or function to be injected

  • args (iterable, optional) – tuple of arguments to be passed to the function

  • kwargs (dict, optional) – dict of keyword arguments to be passed to the function

  • run (bool, optional) – Control immediate execution after injection (default is True)

  • after (before,) – Inject code before or after cell

  • pop (bool) – Pop cell after execution (default is False)

Returns

Injected cell

Return type

TestbookNode

patch(target, **kwargs)

Used as contextmanager to patch objects in the kernel

patch_dict(in_dict, values=(), clear=False, **kwargs)

Used as contextmanager to patch dictionaries in the kernel

ref(name: str) → Union[testbook.reference.TestbookObjectReference, Any]

Return a reference to an object in the kernel

value(code: str) → Any

Execute given code in the kernel and return JSON serializeable result.

If the result is not JSON serializeable, it raises TestbookAttributeError. This error object will also contain an attribute called save_varname which can be used to create a reference object with ref().

Parameters

code (str) – This can be any executable code that returns a value. It can be used the return the value of an object, or the output of a function call.

Returns

Return type

The output of the executed code

Raises

TestbookSerializeError

testbook.exceptions module

exception testbook.exceptions.TestbookAttributeError

Bases: AttributeError

exception testbook.exceptions.TestbookCellTagNotFoundError

Bases: testbook.exceptions.TestbookError

Raised when cell tag is not declared in notebook

exception testbook.exceptions.TestbookError

Bases: Exception

Generic Testbook exception class

exception testbook.exceptions.TestbookExecuteResultNotFoundError

Bases: testbook.exceptions.TestbookError

Raised when there is no execute_result

exception testbook.exceptions.TestbookRuntimeError(evalue, traceback, eclass=None)

Bases: RuntimeError

exception testbook.exceptions.TestbookSerializeError

Bases: testbook.exceptions.TestbookError

Raised when output cannot be JSON serialized