Skip to content

Commit 08a7ae0

Browse files
authored
Merge pull request #18 from blueyed/mm-tests-doctest-mock
Tests: approx: mock doctest runner's pdb usage
2 parents c30f3f7 + 1802986 commit 08a7ae0

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

testing/python/approx.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import doctest
21
import operator
32
from decimal import Decimal
43
from fractions import Fraction
@@ -11,16 +10,34 @@
1110
inf, nan = float("inf"), float("nan")
1211

1312

14-
class MyDocTestRunner(doctest.DocTestRunner):
15-
def __init__(self):
16-
doctest.DocTestRunner.__init__(self)
13+
@pytest.fixture
14+
def mocked_doctest_runner(monkeypatch):
15+
import doctest
1716

18-
def report_failure(self, out, test, example, got):
19-
raise AssertionError(
20-
"'{}' evaluates to '{}', not '{}'".format(
21-
example.source.strip(), got.strip(), example.want.strip()
17+
class MockedPdb:
18+
def __init__(self, out):
19+
pass
20+
21+
def set_trace(self):
22+
raise NotImplementedError("not used")
23+
24+
def reset(self):
25+
pass
26+
27+
def set_continue(self):
28+
pass
29+
30+
monkeypatch.setattr("doctest._OutputRedirectingPdb", MockedPdb)
31+
32+
class MyDocTestRunner(doctest.DocTestRunner):
33+
def report_failure(self, out, test, example, got):
34+
raise AssertionError(
35+
"'{}' evaluates to '{}', not '{}'".format(
36+
example.source.strip(), got.strip(), example.want.strip()
37+
)
2238
)
23-
)
39+
40+
return MyDocTestRunner()
2441

2542

2643
class TestApprox:
@@ -411,13 +428,14 @@ def test_numpy_array_wrong_shape(self):
411428
assert a12 != approx(a21)
412429
assert a21 != approx(a12)
413430

414-
def test_doctests(self):
431+
def test_doctests(self, mocked_doctest_runner):
432+
import doctest
433+
415434
parser = doctest.DocTestParser()
416435
test = parser.get_doctest(
417436
approx.__doc__, {"approx": approx}, approx.__name__, None, None
418437
)
419-
runner = MyDocTestRunner()
420-
runner.run(test)
438+
mocked_doctest_runner.run(test)
421439

422440
def test_unicode_plus_minus(self, testdir):
423441
"""

0 commit comments

Comments
 (0)