-
Notifications
You must be signed in to change notification settings - Fork 136
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Circumvent freezegun in favor of a real clock where possible #228
base: master
Are you sure you want to change the base?
Conversation
Codecov Report
@@ Coverage Diff @@
## master #228 +/- ##
==========================================
- Coverage 99.61% 99.43% -0.18%
==========================================
Files 15 16 +1
Lines 1566 1607 +41
==========================================
+ Hits 1560 1598 +38
- Misses 6 9 +3
Continue to review full report at Codecov.
|
Hi, just bumping this! Re: the coverage checks - I'm happy to follow up with more testing but just wanted to confirm that this change is going in a good direction so far. |
@jombooth - can you please complete the 5 whys exercise for this PR? |
Thanks for your reply! This is maybe the second time I've ever done the "five whys" exercise, so I'll give it a shot but no guarantees this'll be the best representation of the technique:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall, LGTM, but would need to make freezegun dependency optional.
If I merge #231, you may need more work on this PR.
@@ -21,6 +21,7 @@ deps = | |||
djangocurr: django>=2.2.0 | |||
pytest: pytest | |||
lxml>=3.6.0 | |||
freezegun |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be the same as django tests, i.e. if freezegun is not available, don't run those tests. - see other comment.
|
||
import time | ||
|
||
def get_real_time_if_possible(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this snippet is better
try: # pragma: no cover
import freezegun
def real_time():
return freezegun.api.real_time()
except ImportError: # pragma: no cover
def real_time():
return time.time()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: naming with something shorter like real_time
@@ -4,8 +4,10 @@ | |||
|
|||
import xml.etree.ElementTree as ET | |||
from xml.dom.minidom import Document | |||
from freezegun import freeze_time |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unittest-xml-reporting/tests/django_test.py
Lines 11 to 26 in 6556d19
try: | |
import django | |
except ImportError: | |
django = None | |
else: | |
from django.test.utils import get_runner | |
from django.conf import settings, UserSettingsHolder | |
from django.apps import apps | |
settings.configure(DEBUG=True) | |
TESTS_DIR = path.dirname(__file__) | |
@unittest.skipIf(django is None, 'django not found') | |
class DjangoTest(unittest.TestCase): |
please use same kind of approach to not run things that depend on freezegun if it is not available
@jombooth , maybe this could also be the quick fix for you:
|
Hi,
First off, thank you for this wonderful tool! At my work we've been using it to get CircleCI's Test Summary feature to display information about our test runs, and it's been awesome.
I noticed however that for certain test suites where we use Freezegun, the test runner reports a test runtime of 0.000s. I saw in this comment that some prior efforts have been made to circumvent
freezegun
in this project; unfortunately, this approach did not seem to work (at least, not in our case).Following the pseudocode in this pep, I implemented a function that's typically equivalent to authentic
time.time()
, but unlike that function, is not mocked out byfreezegun
. This is now inxmlrunner.time
, and used everywheretime()
was previously called in this project. Where the clock_gettime call it depends on isn't available (mostly, non-Unix systems), or where there is noCLOCK_REALTIME
available on the system, the new function will fall back to (possibly mocked)time.time()
.I also verified in unit tests of the builder and the runner that the resulting timestamps + elapsed time evaluations are accurate even for a test during which time is frozen with
freezegun
- this necessitated addingfreezegun
to the test env intox.ini
.tox -e pytest
works fine with the new tests; I apparently didn't have some of the interpreters running the tests withtox
requires, so that command failed for me locally, but I expect there won't be issues on a properly prepared system.