pytest_notebook.execution module

Execution of notebooks.

exception pytest_notebook.execution.CoverageError[source]

Bases: Exception

Exception for errors involving coverage.

classmethod from_cell_output(phase, output)[source]

Instantiate from a code cell output.

classmethod from_exec_reply(phase, reply)[source]

Instantiate from an execution reply.

class pytest_notebook.execution.ExecuteCoveragePreprocessor(**kw)[source]

Bases: nbconvert.preprocessors.execute.ExecutePreprocessor

A subclass of ExecutePreprocessor that can record python code coverage.

Code coverage is recorded using coverage.py:

  • Before running any cells, we run a mock cell, containing coverage setup code.

  • After execution, we run a mock cell, containing code to teardown the coverage, and print the coverage data to /cell/output/0/text.

  • Coverage data is then saved in resources[“coverage_data”]

Raises

CoverageError – If a coverage cell execution errors.

cov_config_file

A trait for unicode strings.

cov_source

An instance of a Python list.

coverage

A boolean (True, False) trait.

coverage_setup(nb_version)[source]

Set up coverage, by running a code cell.

coverage_teardown(nb_version, resources)[source]

Teardown coverage, by running a code cell, and recording the output.

preprocess(nb, resources, km=None)[source]

Preprocess notebook executing each code cell.

preprocess_cell(cell, resources, cell_index)[source]

Execute a single code cell.

class pytest_notebook.execution.ExecuteResult(exec_error: Union[None, Exception], notebook: nbformat.notebooknode.NotebookNode, resources: dict)[source]

Bases: object

Result of notebook execution.

Parameters
  • exec_error (NoneType, Exception) – Execution exception. This value is accessible, after initialization, via the exec_error attribute.

  • notebook (nbformat.notebooknode.NotebookNode) – Executed notebook. This value is accessible, after initialization, via the notebook attribute.

  • resources (dict) – Resources dictionary. This value is accessible, after initialization, via the resources attribute.

coverage_data(debug=None)[source]

Return coverage.py coverage data as coverage.CoverageData.

property coverage_dict

Return coverage.py coverage data as a dict.

exec_error
property has_coverage

Return whether coverage information is available.

notebook
resources
pytest_notebook.execution.execute_notebook(notebook: nbformat.notebooknode.NotebookNode, *, resources: Optional[dict] = None, cwd: Optional[str] = None, timeout: int = 120, allow_errors: bool = False, with_coverage: bool = False, cov_config_file: Optional[str] = None, cov_source: Optional[List[str]] = None) → pytest_notebook.execution.ExecuteResult[source]

Execute a notebook.

Parameters
  • cwd – Path to the directory which the notebook will run in (default is temporary directory).

  • timeout – The maximum time to wait (in seconds) for execution of each cell.

  • allow_errors – If False, execution is stopped after the first unexpected exception (cells tagged raises-exception are expected)

  • with_coverage – Record code coverage with coverage.py

  • cov_config_file – Determines what coverage configuration file to read.

  • cov_source – A list of file paths or package names to measure coverage for.

Returns

(exception or None, new_notebook, resources)