Skip to content

Commit 6dd2eaa

Browse files
authoredJan 5, 2022
Merge pull request #166 from kanderso-nrel/nbdime_pytest050
Add support for pytest>=0.5.0 to nbdime_reporter.py
2 parents cc10bb4 + 51e6e55 commit 6dd2eaa

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed
 

‎dodo.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def task_test():
2727
}
2828

2929
def task_install_test_deps():
30-
test_deps = ['matplotlib', 'sympy', 'pytest-cov']
30+
test_deps = ['matplotlib', 'sympy', 'pytest-cov', 'pytest-mock', 'nbdime']
3131
return {
3232
'actions': [_make_cmd(['pip', 'install'] + test_deps)],
3333
}

‎nbval/nbdime_reporter.py

+12-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,18 @@
77

88
# import the pytest API
99
import pytest
10-
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
11-
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
10+
try:
11+
from _pytest.main import ExitCode
12+
EXIT_OK = ExitCode.OK
13+
EXIT_TESTSFAILED = ExitCode.TESTS_FAILED
14+
EXIT_INTERRUPTED = ExitCode.INTERRUPTED
15+
EXIT_USAGEERROR = ExitCode.USAGE_ERROR
16+
EXIT_NOTESTSCOLLECTED = ExitCode.NO_TESTS_COLLECTED
17+
except ImportError:
18+
# pytest < 0.5.0
19+
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
20+
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
21+
1222

1323
import re
1424
import copy

‎tests/test_nbdime_reporter.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
2+
from utils import build_nb, add_expected_plaintext_outputs
3+
4+
import nbdime
5+
import nbformat
6+
import os
7+
8+
9+
def test_nbdime(testdir, mocker):
10+
11+
# Make a test notebook where output doesn't match input
12+
nb = build_nb(["1+1", "1+1"], mark_run=True)
13+
add_expected_plaintext_outputs(nb, ["2", "3"])
14+
# Write notebook to test dir
15+
filename = 'test_nbdime.ipynb'
16+
nbformat.write(nb, os.path.join(str(testdir.tmpdir), filename))
17+
18+
# patch the run_server function so that it doesn't actually
19+
# spawn a server and display the diff. But the diff is still
20+
# calculated.
21+
mocker.patch('nbdime.webapp.nbdiffweb.run_server')
22+
result = testdir.runpytest_inprocess('--nbval',
23+
'--nbval-current-env',
24+
'--nbdime',
25+
filename)
26+
# run_server() is only called if there is a discrepancy in the notebook.
27+
# so it should have been called in this case:
28+
nbdime.webapp.nbdiffweb.run_server.assert_called_once()
29+
30+
# note: this import must be AFTER the mocker.patch
31+
from nbval.nbdime_reporter import EXIT_TESTSFAILED
32+
assert result.ret == EXIT_TESTSFAILED

0 commit comments

Comments
 (0)
Please sign in to comment.