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
|
import nbformat
import pytest
from testbook.testbook import testbook
@testbook('tests/resources/inject.ipynb', execute=True)
def test_testbook_execute_all_cells(tb):
for cell in tb.cells[:-1]:
assert cell.execution_count
@testbook('tests/resources/inject.ipynb', execute='hello')
def test_testbook_class_decorator(tb):
assert tb.inject("say_hello()")
@testbook('tests/resources/inject.ipynb')
def test_testbook_class_decorator_execute_none(tb):
assert tb.code_cells_executed == 0
@testbook('tests/resources/inject.ipynb', execute=True)
def test_testbook_decorator_with_fixture(nb, tmp_path):
assert True # Check that the decorator accept to be passed along side a fixture
@testbook('tests/resources/inject.ipynb', execute=True)
@pytest.mark.parametrize("cell_index_args, expected_result", [(2, 2), ('hello', 1)])
def test_testbook_decorator_with_markers(nb, cell_index_args, expected_result):
assert nb._cell_index(cell_index_args) == expected_result
@pytest.mark.parametrize("cell_index_args, expected_result", [(2, 2), ('hello', 1)])
@testbook('tests/resources/inject.ipynb', execute=True)
def test_testbook_decorator_with_markers_order_does_not_matter(nb, cell_index_args, expected_result):
assert nb._cell_index(cell_index_args) == expected_result
def test_testbook_execute_all_cells_context_manager():
with testbook('tests/resources/inject.ipynb', execute=True) as tb:
for cell in tb.cells[:-1]:
assert cell.execution_count
def test_testbook_class_decorator_context_manager():
with testbook('tests/resources/inject.ipynb', execute='hello') as tb:
assert tb.inject("say_hello()")
def test_testbook_class_decorator_execute_none_context_manager():
with testbook('tests/resources/inject.ipynb') as tb:
assert tb.code_cells_executed == 0
def test_testbook_with_file_object():
f = open('tests/resources/inject.ipynb')
with testbook(f) as tb:
assert tb.code_cells_executed == 0
f.close()
def test_testbook_with_notebook_node():
nb = nbformat.read('tests/resources/inject.ipynb', as_version=4)
with testbook(nb) as tb:
assert tb.code_cells_executed == 0
|