File: conftest.py

package info (click to toggle)
python-testbook 0.4.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 408 kB
  • sloc: python: 1,045; makefile: 11
file content (46 lines) | stat: -rw-r--r-- 1,503 bytes parent folder | download | duplicates (2)
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
from typing import List, Optional

import pytest
from jupyter_client import kernelspec
from nbformat.notebooknode import NotebookNode
from nbformat.v4 import new_notebook, new_code_cell, new_output


@pytest.fixture
def notebook_factory():
    """Pytest fixture to generate a valid notebook."""

    def notebook_generator(cells: Optional[List[NotebookNode]] = None) -> NotebookNode:
        """Generate an executable notebook.

        The notebook cells are the one passed as arguments or the hard-coded cells
        if no cells is provided.
        """
        metadata = {}
        for name in kernelspec.find_kernel_specs():
            ks = kernelspec.get_kernel_spec(name)
            metadata = {
                'kernelspec': {
                    'name': name,
                    'language': ks.language,
                    'display_name': ks.display_name,
                }
            }
            break

        if cells is not None:
            all_cells = cells
        else:  # Default cells
            all_cells = [
                new_code_cell('a = 2', metadata={"tags": []}),
                new_code_cell('b=22\nb', metadata={"tags": ["test"]}),
                new_code_cell(
                    "",
                    metadata={"tags": ["dummy-outputs"]},
                    outputs=[new_output('execute_result', data={"text/plain": "text"})],
                ),
            ]

        return new_notebook(metadata=metadata, cells=all_cells)

    return notebook_generator