Created
March 11, 2017 04:57
-
-
Save AndyLPK247/c37e829a71cf7a3b3bd3cda74c7656a7 to your computer and use it in GitHub Desktop.
Python Testing 101 doctest Example: Doctests in Docstrings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def add(a, b): | |
""" | |
Adds two numbers. | |
>>> add(3, 2) | |
5 | |
""" | |
return a + b | |
def subtract(a, b): | |
""" | |
Subtracts two numbers. | |
>>> subtract(3, 2) | |
1 | |
>>> subtract(2, 3) | |
-1 | |
""" | |
return a - b | |
def multiply(a, b): | |
""" | |
Multiplies two numbers. | |
>>> multiply(3, 2) | |
6 | |
""" | |
return a * b | |
def divide(a, b): | |
""" | |
Divides two numbers. | |
Automatically raises ZeroDivisionError. | |
>>> divide(3.0, 2.0) | |
1.5 | |
>>> divide(1.0, 0) | |
Traceback (most recent call last): | |
... | |
ZeroDivisionError: float division by zero | |
""" | |
return a * 1.0 / b | |
def maximum(a, b): | |
""" | |
Finds the maximum of two numbers. | |
>>> maximum(3, 2) | |
3 | |
>>> maximum(2, 3) | |
3 | |
>>> maximum(3, 3) | |
3 | |
""" | |
return a if a >= b else b | |
def minimum(a, b): | |
""" | |
Finds the minimum of two numbers. | |
>>> minimum(3, 2) | |
2 | |
>>> minimum(2, 3) | |
2 | |
>>> minimum(2, 2) | |
2 | |
""" | |
return a if a <= b else b |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment