doctest random output?

Leam Hall leamhall at gmail.com
Mon Aug 28 05:41:19 EDT 2017


Is this a good way to test if random numeric output? It seems to work 
under Python 2.6 and 3.6 but that doesn't make it 'good'.

###  Code
import random

def my_thing():
   """ Return a random number from 1-6
   >>> 0 < my_thing() <=6
   True
   >>> 6 < my_thing()
   False
   """

   return random.randint(1,6)


if __name__ == "__main__":
   import doctest
   doctest.testmod()


###  Results
python3 test_doctest.py -v
Trying:
     0 < my_thing() <=6
Expecting:
     True
ok
Trying:
     6 < my_thing()
Expecting:
     False
ok
1 items had no tests:
     __main__
1 items passed all tests:
    2 tests in __main__.my_thing
2 tests in 2 items.
2 passed and 0 failed.
Test passed.




More information about the Python-list mailing list