FreezeGun: easy mocking of Python datetimes
stevepulec.com
FreezeGun: easy mocking of Python datetimes
1–10 of 18 posts
Re: FreezeGun: easy mocking of Python datetimes
#2Pure functions make tests simple.
Re: FreezeGun: easy mocking of Python datetimes
#3https://github.com/travisjeffery/timecop
https://github.com/bebanjo/delorean
In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.
Re: FreezeGun: easy mocking of Python datetimes
#4Rubyists desiring similar functionality should check out either of the following libs. As far as I can tell they differ only in their pun selection. https://github.com/travisjeffery/timecop https://github.com/bebanjo/delorean In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.
Hmm, perhaps I should check Ruby out after all...
Re: FreezeGun: easy mocking of Python datetimes
#5Re: FreezeGun: easy mocking of Python datetimes
#6Cool, but unfortunately it breaks if the code your testing does from datetime import datetime :( https://gist.github.com/4261936
Re: FreezeGun: easy mocking of Python datetimes
#7Rubyists desiring similar functionality should check out either of the following libs. As far as I can tell they differ only in their pun selection. https://github.com/travisjeffery/timecop https://github.com/bebanjo/delorean In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite.
>In the ruby community, disliking someone's choice of science fiction reference is grounds for a rewrite. Hmm, perhaps I should check Ruby out after all...
Re: FreezeGun: easy mocking of Python datetimes
#8Cool, but unfortunately it breaks if the code your testing does from datetime import datetime :( https://gist.github.com/4261936
Hey Timothy, thanks for the response. You are correct. I'll add a warning to the library and work on a solution. Unfortunately, I think it will need to involve ctypes.
import freezegun; freezegun.monkey_patch()
from datetime import datetime
After that, freeze_time would just set a flag on your datetime class.Re: FreezeGun: easy mocking of Python datetimes
#9Earlier quoted context omitted.
Hey Timothy, thanks for the response. You are correct. I'll add a warning to the library and work on a solution. Unfortunately, I think it will need to involve ctypes.
Have you tried patching the now() method onto datetime.datetime instead of replacing the whole class? If that doesn't work, you could replace datetime first: import freezegun; freezegun.monkey_patch() from datetime import datetime After that, freeze_time would just set a flag on your datetime class.
Re: FreezeGun: easy mocking of Python datetimes
#10Earlier quoted context omitted.
Have you tried patching the now() method onto datetime.datetime instead of replacing the whole class? If that doesn't work, you could replace datetime first: import freezegun; freezegun.monkey_patch() from datetime import datetime After that, freeze_time would just set a flag on your datetime class.
Sadly, it's not possible. datetime is a C class, so it is immutable.