Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#23792 closed Cleanup/optimization (fixed)

Create contextmanager to freeze time.time() in tests

Reported by: Thomas C Owned by: nobody
Component: Testing framework Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Thomas C)

Change History (8)

comment:1 by Thomas C, 10 years ago

Description: modified (diff)

comment:2 by Tim Graham, 10 years ago

Component: UncategorizedTesting framework
Summary: Random test failure of TestCookieStorage.test_reset_cookieCreate contextmanager to freeze time.time() in tests
Triage Stage: UnreviewedAccepted

The contextmanager approach looks good, but formtools is being split into a separate repo (#23677). If formtools is going to use this, then I guess we should make this a public API. What do you think?

I created a ticket in the django-formtools issue tracker.

comment:3 by Jannis Leidel, 10 years ago

Someone proposed freezegun in the PR (https://github.com/spulec/freezegun), which seems like the more complete approach than this local monkey patch. So to me the question is whether we accept the limitation of the context manager since it's a test utility but should point at freezegun as well?

For formtools I'd like to follow whatever we decide for Django and then backport it for versions that support older Djangos.

comment:4 by Tim Graham, 10 years ago

Our current policy for test dependencies is to skip any tests when the dependencies aren't installed, so I'm not sure if adding a dependency is worth it for the affected tests. On the other hand, if freezegun could be used more often in our tests (haven't looked), then maybe it's worth trying to change the policy and make it a hard dependency. Making mock a hard dependency was proposed in #23289. I think the state of virtualenv, etc. is at a point where we could make this a requirement, but I'll write to the mailing list about it if there are no immediate objections here.

comment:5 by Jannis Leidel, 10 years ago

Makes sense to me, thanks Tim!

comment:6 by Tim Graham, 10 years ago

Patch needs improvement: set

Marking as "patch needs improvement" to indicate the needed research as to whether or not to use freezegun (commented on the PR about this).

comment:7 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 994d6137a2519436d17d5b3d16cb49a3fa79f93e:

Fixed #23792 -- Added test.utils.freeze_time() context manager.

comment:8 by Tim Graham, 10 years ago

Just to summarize the conclusions on the pull request, we decided that adding freezegun as a dependency wasn't worth the two tests that could use it. The freeze_time() context manager has some limitations but is fine for Django's test suite and should be fine to use in formtools tests even though we didn't make it a public API. I don't think the fact that we'd like to use it their justifies bypassing our normal backport policy and and adding it to 1.7 though.

Note: See TracTickets for help on using tickets.
Back to Top