Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 14e00b1

Browse files
committedSep 11, 2020
Use subprocess for running notebooks.
1 parent ebb1d67 commit 14e00b1

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed
 

‎tests/test_magics.py

+15-25
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,25 @@
99

1010
pytest_plugins = "pytester"
1111

12-
@pytest.mark.parametrize("magic, expected_passes", [
13-
(r"%dirs", [True]),
14-
(r"%precision bad", [False]),
15-
(r"%this_magic_does_not_exist", [False])
12+
@pytest.mark.parametrize("magic, expected_pass", [
13+
(r"%dirs", True),
14+
(r"%precision bad", False),
15+
(r"%this_magic_does_not_exist", False)
1616
])
17-
def test_magics(testdir, magic, expected_passes):
18-
# Setup notebook to test:
19-
sources = [
17+
def test_magics(testdir, magic, expected_pass):
18+
nb = build_nb([
2019
# In [1]:
2120
magic,
22-
]
23-
nb = build_nb(sources)
24-
25-
# Write notebook to test dir
21+
])
22+
nb_name = 'test_magics'
2623
nbformat.write(nb, os.path.join(
27-
str(testdir.tmpdir), 'test_magics.ipynb'))
28-
29-
# Run tests
30-
result = testdir.inline_run('--nbval-lax', '--current-env', '-s')
31-
reports = result.getreports('pytest_runtest_logreport')
32-
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
24+
str(testdir.tmpdir), nb_name+".ipynb"))
3725

38-
reports = [r for r in reports if r.when == 'call']
26+
# using subprocess because otherwise second and subsequent tests always fail (some state left over somewhere in the jupyter stack)
27+
result = testdir.runpytest_subprocess('--nbval-lax', '--current-env', '-s', '-v')
3928

40-
assert len(reports) == len(expected_passes)
29+
assert result.ret == (not expected_pass)
4130

42-
for actual_report, expected_pass in zip(reports, expected_passes):
43-
assert actual_report.passed is expected_pass
31+
result.stdout.fnmatch_lines_random(
32+
["*collected 1 item*",
33+
"{nb_name}::ipynb::Cell 0 {result}".format(nb_name=nb_name, result="PASSED" if expected_pass else "FAILED")])

0 commit comments

Comments
 (0)
Please sign in to comment.