Skip to content

Commit

Permalink
Merge pull request #166 from kanderso-nrel/nbdime_pytest050
Browse files Browse the repository at this point in the history
Add support for pytest>=0.5.0 to nbdime_reporter.py
  • Loading branch information
takluyver authored Jan 5, 2022
2 parents cc10bb4 + 51e6e55 commit 6dd2eaa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def task_test():
}

def task_install_test_deps():
test_deps = ['matplotlib', 'sympy', 'pytest-cov']
test_deps = ['matplotlib', 'sympy', 'pytest-cov', 'pytest-mock', 'nbdime']
return {
'actions': [_make_cmd(['pip', 'install'] + test_deps)],
}
Expand Down
14 changes: 12 additions & 2 deletions nbval/nbdime_reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,18 @@

# import the pytest API
import pytest
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED
try:
from _pytest.main import ExitCode
EXIT_OK = ExitCode.OK
EXIT_TESTSFAILED = ExitCode.TESTS_FAILED
EXIT_INTERRUPTED = ExitCode.INTERRUPTED
EXIT_USAGEERROR = ExitCode.USAGE_ERROR
EXIT_NOTESTSCOLLECTED = ExitCode.NO_TESTS_COLLECTED
except ImportError:
# pytest < 0.5.0
from _pytest.main import EXIT_OK, EXIT_TESTSFAILED, EXIT_INTERRUPTED, \
EXIT_USAGEERROR, EXIT_NOTESTSCOLLECTED


import re
import copy
Expand Down
32 changes: 32 additions & 0 deletions tests/test_nbdime_reporter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

from utils import build_nb, add_expected_plaintext_outputs

import nbdime
import nbformat
import os


def test_nbdime(testdir, mocker):

# Make a test notebook where output doesn't match input
nb = build_nb(["1+1", "1+1"], mark_run=True)
add_expected_plaintext_outputs(nb, ["2", "3"])
# Write notebook to test dir
filename = 'test_nbdime.ipynb'
nbformat.write(nb, os.path.join(str(testdir.tmpdir), filename))

# patch the run_server function so that it doesn't actually
# spawn a server and display the diff. But the diff is still
# calculated.
mocker.patch('nbdime.webapp.nbdiffweb.run_server')
result = testdir.runpytest_inprocess('--nbval',
'--nbval-current-env',
'--nbdime',
filename)
# run_server() is only called if there is a discrepancy in the notebook.
# so it should have been called in this case:
nbdime.webapp.nbdiffweb.run_server.assert_called_once()

# note: this import must be AFTER the mocker.patch
from nbval.nbdime_reporter import EXIT_TESTSFAILED
assert result.ret == EXIT_TESTSFAILED

0 comments on commit 6dd2eaa

Please sign in to comment.