Skip to content

Instantly share code, notes, and snippets.

@jrgavilanes
Created February 7, 2023 18:16
Show Gist options
  • Save jrgavilanes/799c1daf226fd28a8bd24608dd771041 to your computer and use it in GitHub Desktop.
Save jrgavilanes/799c1daf226fd28a8bd24608dd771041 to your computer and use it in GitHub Desktop.
python example of function with docstring that does selfcontained unit tests
def suma(a: int = None, b: int = None) -> int:
""" sums two given integers
Args:
a: int
b: int
return:
int
>>> suma(2,4)
6
>>> suma(2)
Traceback (most recent call last):
Exception: parameters are not given
>>> suma()
Traceback (most recent call last):
Exception: parameters are not given
"""
if not (a and b):
raise Exception("parameters are not given")
return a + b
*************
$ python -m doctest function.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment