Merge pull request #6071 from blueyed/tests-doctest-mock

Tests: approx: mock doctest runner's pdb usage
This commit is contained in:
Daniel Hahler 2019-11-06 13:27:50 +01:00 committed by GitHub
commit 0485b07ff0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 12 deletions

View File

@ -1,4 +1,3 @@
import doctest
import operator
from decimal import Decimal
from fractions import Fraction
@ -11,16 +10,34 @@ 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):
import doctest
def report_failure(self, out, test, example, got):
raise AssertionError(
"'{}' evaluates to '{}', not '{}'".format(
example.source.strip(), got.strip(), example.want.strip()
class MockedPdb:
def __init__(self, out):
pass
def set_trace(self):
raise NotImplementedError("not used")
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 +428,14 @@ class TestApprox:
assert a12 != approx(a21)
assert a21 != approx(a12)
def test_doctests(self):
def test_doctests(self, mocked_doctest_runner):
import doctest
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):
"""