From 32412532ef58fbaf7a43a766e20fbf94da21feee Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 27 Oct 2019 02:34:24 +0200 Subject: [PATCH 1/2] tests: mock doctest.DocTestRunner to not use real pdb It is not used there anyway, and might cause false positives. --- testing/python/approx.py | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) diff --git a/testing/python/approx.py b/testing/python/approx.py index 60fde151a..11502c509 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -11,16 +11,32 @@ from pytest import approx inf, nan = float("inf"), float("nan") -class MyDocTestRunner(doctest.DocTestRunner): - def __init__(self): - doctest.DocTestRunner.__init__(self) +@pytest.fixture +def mocked_doctest_runner(monkeypatch): + class MockedPdb: + def __init__(self, out): + pass - def report_failure(self, out, test, example, got): - raise AssertionError( - "'{}' evaluates to '{}', not '{}'".format( - example.source.strip(), got.strip(), example.want.strip() + def set_trace(self): + pass + + def reset(self): + pass + + def set_continue(self): + pass + + monkeypatch.setattr("doctest._OutputRedirectingPdb", MockedPdb) + + class MyDocTestRunner(doctest.DocTestRunner): + def report_failure(self, out, test, example, got): + raise AssertionError( + "'{}' evaluates to '{}', not '{}'".format( + example.source.strip(), got.strip(), example.want.strip() + ) ) - ) + + return MyDocTestRunner() class TestApprox: @@ -411,13 +427,12 @@ class TestApprox: assert a12 != approx(a21) assert a21 != approx(a12) - def test_doctests(self): + def test_doctests(self, mocked_doctest_runner): parser = doctest.DocTestParser() test = parser.get_doctest( approx.__doc__, {"approx": approx}, approx.__name__, None, None ) - runner = MyDocTestRunner() - runner.run(test) + mocked_doctest_runner.run(test) def test_unicode_plus_minus(self, testdir): """ From a5bd19e3b406f45ffaadf7e1d257b90f11e5931f Mon Sep 17 00:00:00 2001 From: Daniel Hahler Date: Sun, 27 Oct 2019 02:37:13 +0200 Subject: [PATCH 2/2] tests: lazily import doctest in approx tests --- testing/python/approx.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/testing/python/approx.py b/testing/python/approx.py index 11502c509..f72045624 100644 --- a/testing/python/approx.py +++ b/testing/python/approx.py @@ -1,4 +1,3 @@ -import doctest import operator from decimal import Decimal from fractions import Fraction @@ -13,12 +12,14 @@ inf, nan = float("inf"), float("nan") @pytest.fixture def mocked_doctest_runner(monkeypatch): + import doctest + class MockedPdb: def __init__(self, out): pass def set_trace(self): - pass + raise NotImplementedError("not used") def reset(self): pass @@ -428,6 +429,8 @@ class TestApprox: assert a21 != approx(a12) def test_doctests(self, mocked_doctest_runner): + import doctest + parser = doctest.DocTestParser() test = parser.get_doctest( approx.__doc__, {"approx": approx}, approx.__name__, None, None