1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
"""Test doctest integration and flexmock API examples."""
# pylint: disable=missing-docstring
import doctest
import sys
from flexmock import _api
class Plane:
model = "Airpy 1"
is_flying = True
def __init__(self):
self.speed = 3
self.direction = None
self.destination = None
self.passengers = ["user1", "user2", "user3"]
def pilot(self):
return self.passengers[0]
def fly(self, direction="up", destination="Helsinki"):
self.direction = direction
self.destination = destination
def land(self):
return None
def set_speed(self, speed):
self.speed = speed
def repair(self, part):
del part
def crash(self):
return None
def flight_log(self):
yield from ["land", "repair"]
def passenger_count(self):
return len(self.passengers)
class BadWeatherException(Exception):
pass
class TestDoctestTeardown:
"""Test doctest runner teardown."""
def test_flexmock_teardown_works_with_doctest_part_1(self):
"""Part 1
Examples:
Part 1:
>>> from flexmock import flexmock
>>> flexmock().should_receive("method1").ordered()
<flexmock._api.Expectation object at ...>
"""
def test_flexmock_teardown_works_with_doctest_part_2(self):
"""Raises CallOrderError if flexmock teardown is not automatically called
after test part 1 above.
Examples:
Part 2:
>>> from flexmock import flexmock
>>> mock = flexmock().should_receive("method2").ordered().mock()
>>> mock.method2()
"""
if __name__ == "__main__":
results1 = doctest.testmod(
sys.modules[__name__], # current module
optionflags=doctest.ELLIPSIS,
)
results2 = doctest.testmod(
_api,
extraglobs={
"Plane": Plane,
"plane": Plane(),
"BadWeatherException": BadWeatherException,
},
optionflags=doctest.ELLIPSIS,
)
sys.exit(bool(results1.failed + results2.failed))
|