doctest-example.py 409 B

123456789101112131415161718192021222324
  1. #! /usr/bin/python3
  2. #
  3. # This example shows some built in unit tests for functions, alone with
  4. # docstring examples.
  5. def square(x):
  6. """
  7. Returns x times itself.
  8. >>> square(2)
  9. 4
  10. >>> square(4)
  11. 16
  12. >>> square(333)
  13. 110889
  14. """
  15. return x*x
  16. if __name__ == "__main__":
  17. """
  18. This is how doctest would be used in a module.
  19. """
  20. import doctest
  21. doctest.testmod()