We'll embed a test right inside the Python source file that it tests, by placing it inside a docstring.
test.pywith the following contents:
def testable(x): r""" The `testable` function returns the square root of its parameter, or 3, whichever is larger. >>> testable(7) 3.0 >>> testable(16) 4.0 >>> testable(9) 3.0 >>> testable(10) == 10 ** 0.5 True """ if x < 9: return 3.0 return x ** 0.5
test.pyand then run the tests by typing:
$ python ‑m doctest test.py
As mentioned earlier before, if you have an older version of Python, this isn't going to work for you. Instead, you need to type
python ‑c "__import__('doctest').testmod(__import__('test'))" ...