|
9 | 9 | pytest_plugins = "pytester"
|
10 | 10 |
|
11 | 11 |
|
12 |
| -def test_unrun_raises(testdir): |
| 12 | +def test_run_raises(testdir): |
13 | 13 | # This test uses the testdir fixture from pytester, which is useful for
|
14 | 14 | # testing pytest plugins. It writes a notebook to a temporary dir
|
15 | 15 | # and then runs pytest.
|
16 | 16 |
|
17 | 17 | # Setup notebook to test:
|
18 | 18 | sources = [
|
19 | 19 | # 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 |
21 | 25 | ]
|
22 | 26 | # 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 | + ) |
24 | 58 |
|
25 | 59 | # Write notebook to test dir
|
26 | 60 | nbformat.write(nb, os.path.join(
|
27 | 61 | str(testdir.tmpdir), 'test_expcted_exceptions.ipynb'))
|
28 | 62 |
|
29 | 63 | # 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) |
32 | 66 |
|
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 |
37 | 67 |
|
38 |
| - reports = [r for r in reports if r.when == 'call'] |
39 | 68 |
|
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. |
41 | 73 |
|
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