freezegun
freezegun copied to clipboard
How do I use freezegun in doctest?
is there any best practice?
Explicitly? As in put a with block to freeze the time. That seems pretty reasonable.
One could also imagine globally freezing the time if you have lots of these doctests in your code base. That's what we do by default at work and it's very nice.
Leaving it here for the next person:
you can copy and paste all the code straight to into the terminal
def func(a, b):
"""
>>> import freezegun
>>> with freezegun.freeze_time('2018-01-01'):
... func(5, 5)
10
"""
return a + b
and run it:
import doctest
doctest.testmod()