-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathtest_expected_exceptions.py
89 lines (71 loc) · 2.42 KB
/
test_expected_exceptions.py
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
import os
import nbformat
import pytest
from utils import build_nb
pytest_plugins = "pytester"
def test_run_raises(testdir):
# This test uses the testdir fixture from pytester, which is useful for
# testing pytest plugins. It writes a notebook to a temporary dir
# and then runs pytest.
# Setup notebook to test:
sources = [
# In [1]:
"", # No error produced, when one is expected
# In [2]:
"raise ValueError('foo')", # Wrong ename
# In [3]:
"raise ValueError('foo')", # Wrong evalue
]
# Build unrun notebook:
nb = build_nb(sources, mark_run=True)
nb.cells[0].metadata.tags = ['raises-exception']
nb.cells[0].outputs.append(
nbformat.v4.new_output(
'error',
ename='ValueError',
evalue='foo',
traceback=['foobar', 'bob'], # Should be ignored
)
)
nb.cells[1].metadata.tags = ['raises-exception']
nb.cells[1].outputs.append(
nbformat.v4.new_output(
'error',
ename='TypeError', # Expected TypeError, got ValueError
evalue='foo',
traceback=['foobar', 'bob'], # Should be ignored
)
)
nb.cells[2].metadata.tags = ['raises-exception']
nb.cells[2].outputs.append(
nbformat.v4.new_output(
'error',
ename='ValueError',
evalue='bar', # Expected bar, got foo
traceback=['foobar', 'bob'], # Should be ignored
)
)
# Write notebook to test dir
nbformat.write(nb, os.path.join(
str(testdir.tmpdir), 'test_expcted_exceptions.ipynb'))
# Run tests
result = testdir.runpytest_subprocess('--nbval', '--current-env', '-s')
result.assert_outcomes(failed=3)
def test_unrun_raises(testdir):
# This test uses the testdir fixture from pytester, which is useful for
# testing pytest plugins. It writes a notebook to a temporary dir
# and then runs pytest.
# Setup notebook to test:
sources = [
# In [1]:
"pass",
]
# Build unrun notebook:
nb = build_nb(sources, mark_run=False)
nb.cells[0].metadata.tags = ['raises-exception']
# Write notebook to test dir
nbformat.write(nb, os.path.join(
str(testdir.tmpdir), 'test_expcted_exceptions.ipynb'))
# Run tests
result = testdir.runpytest_subprocess('--nbval', '--current-env', '-s')
result.assert_outcomes(failed=1)