Skip to content

Commit a124c50

Browse files
committed
Fix/expand tests for expected failures
1 parent 0533015 commit a124c50

File tree

1 file changed

+59
-13
lines changed

1 file changed

+59
-13
lines changed

tests/test_expected_exceptions.py

+59-13
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,81 @@
99
pytest_plugins = "pytester"
1010

1111

12-
def test_unrun_raises(testdir):
12+
def test_run_raises(testdir):
1313
# This test uses the testdir fixture from pytester, which is useful for
1414
# testing pytest plugins. It writes a notebook to a temporary dir
1515
# and then runs pytest.
1616

1717
# Setup notebook to test:
1818
sources = [
1919
# In [1]:
20-
"raise ValueError('foo')",
20+
"", # No error produced, when one is expected
21+
# In [2]:
22+
"raise ValueError('foo')", # Wrong ename
23+
# In [3]:
24+
"raise ValueError('foo')", # Wrong evalue
2125
]
2226
# Build unrun notebook:
23-
nb = build_nb(sources, mark_run=False)
27+
nb = build_nb(sources, mark_run=True)
28+
29+
nb.cells[0].metadata.tags = ['raises-exception']
30+
nb.cells[0].outputs.append(
31+
nbformat.v4.new_output(
32+
'error',
33+
ename='ValueError',
34+
evalue='foo',
35+
traceback=['foobar', 'bob'], # Should be ignored
36+
)
37+
)
38+
39+
nb.cells[1].metadata.tags = ['raises-exception']
40+
nb.cells[1].outputs.append(
41+
nbformat.v4.new_output(
42+
'error',
43+
ename='TypeError', # Expected TypeError, got ValueError
44+
evalue='foo',
45+
traceback=['foobar', 'bob'], # Should be ignored
46+
)
47+
)
48+
49+
nb.cells[2].metadata.tags = ['raises-exception']
50+
nb.cells[2].outputs.append(
51+
nbformat.v4.new_output(
52+
'error',
53+
ename='ValueError',
54+
evalue='bar', # Expected bar, got foo
55+
traceback=['foobar', 'bob'], # Should be ignored
56+
)
57+
)
2458

2559
# Write notebook to test dir
2660
nbformat.write(nb, os.path.join(
2761
str(testdir.tmpdir), 'test_expcted_exceptions.ipynb'))
2862

2963
# Run tests
30-
result = testdir.inline_run('--nbval', '--current-env', '-s')
31-
reports = result.getreports('pytest_runtest_logreport')
64+
result = testdir.runpytest_subprocess('--nbval', '--current-env', '-s')
65+
result.assert_outcomes(failed=3)
3266

33-
# Setup and teardown of cells should have no issues:
34-
setup_teardown = [r for r in reports if r.when != 'call']
35-
for r in setup_teardown:
36-
assert r.passed
3767

38-
reports = [r for r in reports if r.when == 'call']
3968

40-
assert len(reports) == 1
69+
def test_unrun_raises(testdir):
70+
# This test uses the testdir fixture from pytester, which is useful for
71+
# testing pytest plugins. It writes a notebook to a temporary dir
72+
# and then runs pytest.
4173

42-
# First cell should fail, unexpectedly
43-
assert reports[0].failed and not hasattr(reports[0], 'wasxfail')
74+
# Setup notebook to test:
75+
sources = [
76+
# In [1]:
77+
"pass",
78+
]
79+
# Build unrun notebook:
80+
nb = build_nb(sources, mark_run=False)
81+
nb.cells[0].metadata.tags = ['raises-exception']
82+
83+
# Write notebook to test dir
84+
nbformat.write(nb, os.path.join(
85+
str(testdir.tmpdir), 'test_expcted_exceptions.ipynb'))
86+
87+
# Run tests
88+
result = testdir.runpytest_subprocess('--nbval', '--current-env', '-s')
89+
result.assert_outcomes(failed=1)

0 commit comments

Comments
 (0)