File: test_execute.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 (102 lines) | stat: -rw-r--r-- 2,834 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import pytest

from testbook.testbook import testbook
from testbook.exceptions import TestbookRuntimeError, TestbookError


@pytest.fixture(scope='module')
def notebook():
    with testbook('tests/resources/foo.ipynb', execute=True) as tb:
        yield tb


def test_execute_cell(notebook):
    notebook.execute_cell(1)
    assert notebook.cell_output_text(1) == 'hello world\n[1, 2, 3]'

    notebook.execute_cell([2, 3])
    assert notebook.cell_output_text(3) == 'foo'


def test_execute_and_show_pandas_output(notebook):
    notebook.execute_cell(4)
    assert notebook.cell_output_text(4) == """col1  col2
0     1     3
1     2     4"""


def test_execute_cell_tags(notebook):
    notebook.execute_cell('test1')
    assert notebook.cell_output_text('test1') == 'hello world\n[1, 2, 3]'

    notebook.execute_cell(['prepare_foo', 'execute_foo'])
    assert notebook.cell_output_text('execute_foo') == 'foo'


def test_execute_cell_raises_error(notebook):
    with pytest.raises(TestbookRuntimeError):
        try:
            notebook.inject("1/0", pop=True)
        except TestbookRuntimeError as e:
            assert e.eclass == ZeroDivisionError
            raise


def test_testbook_with_execute(notebook):
    notebook.execute_cell('execute_foo')
    assert notebook.cell_output_text('execute_foo') == 'foo'


def test_testbook_with_execute_context_manager(notebook):
    notebook.execute_cell('execute_foo')
    assert notebook.cell_output_text('execute_foo') == 'foo'


def test_testbook_range():
    with testbook('tests/resources/inject.ipynb') as tb:
        tb.execute_cell(range(4))
        assert tb.code_cells_executed == 4

    with testbook('tests/resources/inject.ipynb', execute=range(4)) as tb:
        assert tb.code_cells_executed == 4


@pytest.mark.parametrize("slice_params, expected_result", [(('hello', 'str'), 6), ((2, 5), 4)])
def test_testbook_slice(slice_params, expected_result):
    with testbook('tests/resources/inject.ipynb') as tb:
        tb.execute_cell(slice(*slice_params))
        assert tb.code_cells_executed == expected_result

    with testbook('tests/resources/inject.ipynb', execute=slice(*slice_params)) as tb:
        assert tb.code_cells_executed == expected_result


def test_testbook_slice_raises_error():
    with pytest.raises(TestbookError):
        with testbook('tests/resources/inject.ipynb', execute=slice(3, 1, -1)):
            pass


@testbook('tests/resources/exception.ipynb', execute=True)
def test_raise_exception(tb):
    with pytest.raises(TestbookRuntimeError):
        tb.ref("raise_my_exception")()


@testbook('tests/resources/inject.ipynb')
def test_underscore(tb):
    tb.inject(
        """
        _ = 20

        def foo(x):
            return x + 1
    """,
        run=False,
    )

    tb.execute()

    foo = tb.ref("foo")

    assert foo(2) == 3