#23792 closed Cleanup/optimization (fixed)
Create contextmanager to freeze time.time() in tests
Description (last modified by ) ¶
This test fails randomly, as seen in http://djangoci.com/job/django-pull-requests/1528/database=postgres,python=python3.4/testReport/junit/django.contrib.formtools.tests.wizard.test_cookiestorage/TestCookieStorage/test_reset_cookie/
After some investigation, I can reproduce the failure by adding a time.sleep(1)
after this line https://github.com/django/django/blob/88b2a20f047347da86f448fe09a56251d29e4168/django/core/signing.py#L183.
Change History (8)
comment:1 by , 10 years ago
Description: | modified (diff) |
---|
comment:2 by , 10 years ago
Component: | Uncategorized → Testing framework |
---|---|
Summary: | Random test failure of TestCookieStorage.test_reset_cookie → Create contextmanager to freeze time.time() in tests |
Triage Stage: | Unreviewed → Accepted |
comment:3 by , 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 , 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:6 by , 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 , 10 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
comment:8 by , 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.
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.