From 04383b2fbdcd7ae0dce8ae36f1c20fa7cf7716bd Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Tue, 6 Apr 2021 11:31:08 -0600 Subject: [PATCH 1/3] try/except for pytest exit code import --- nbval/nbdime_reporter.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nbval/nbdime_reporter.py b/nbval/nbdime_reporter.py index 3eaa031..64871a2 100644 --- a/nbval/nbdime_reporter.py +++ b/nbval/nbdime_reporter.py @@ -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 From 6ef1d3e3b8a3da0f8bbb0899becd10b8db515789 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Tue, 4 Jan 2022 08:54:47 -0500 Subject: [PATCH 2/3] add nbdime test --- setup.py | 1 + tests/test_nbdime_reporter.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 tests/test_nbdime_reporter.py diff --git a/setup.py b/setup.py index 3448f98..278a84d 100644 --- a/setup.py +++ b/setup.py @@ -22,6 +22,7 @@ }, install_requires = [ 'pytest >= 2.8', + 'pytest-mock', 'jupyter_client', 'nbformat', 'ipykernel', diff --git a/tests/test_nbdime_reporter.py b/tests/test_nbdime_reporter.py new file mode 100644 index 0000000..cb5ab7b --- /dev/null +++ b/tests/test_nbdime_reporter.py @@ -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 From 51e6e5583a5d208b5f60ad56dd2e25c57c7a36a2 Mon Sep 17 00:00:00 2001 From: Kevin Anderson Date: Tue, 4 Jan 2022 14:33:19 -0500 Subject: [PATCH 3/3] fix test dependencies --- dodo.py | 2 +- setup.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/dodo.py b/dodo.py index f9c5cf8..daf0906 100644 --- a/dodo.py +++ b/dodo.py @@ -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)], } diff --git a/setup.py b/setup.py index 278a84d..3448f98 100644 --- a/setup.py +++ b/setup.py @@ -22,7 +22,6 @@ }, install_requires = [ 'pytest >= 2.8', - 'pytest-mock', 'jupyter_client', 'nbformat', 'ipykernel',