Questions tagged [freezegun]

a library that allows your python tests to travel through time by mocking the datetime module

24 questions
12
votes
1 answer

Using freezegun, why do pytz.utc and utcnow() output different datetimes?

I'm puzzled why a function that freezes time with freezegun outputs different UTC times depending on whether datetime.datetime.utcnow() is called, or datetime.datetime.now(pytz.utc). I'm not saying it's broken, just that I don't understand why, and…
Phil Gyford
  • 13,432
  • 14
  • 81
  • 143
7
votes
1 answer

Python Dataclasses: Mocking the default factory in a frozen Dataclass

I'm attempting to use freezegun in my unit tests to patch a field in a dataclass that is set to the current date when the object is initialised. I would imagine the question is relevant to any attempt to patch a function being used as a…
Steven
  • 823
  • 8
  • 25
7
votes
2 answers

Mocking time issue in django test: time seems not to be frozen using freezegun

I'm writing here a functional test to check if my API throttling is working as expected (will be rest at the beginning of every month). Testing Class: class ApiThrottlingTest(ThrottlingBaseTest): def test_throttling_purchaser_case(self): …
Dhia
  • 10,119
  • 11
  • 58
  • 69
6
votes
1 answer

time freezing doesn't work with pandas timestamps

I've noticed this behaviour since at least 2015 and it hasn't changed since. When freezegun (or pytest-freezegun) is used to freeze time in a test, datetime.datetime.now() returns the frozen value while pd.Timestamp('now') and pd.to_datetime('now')…
s5s
  • 11,159
  • 21
  • 74
  • 121
6
votes
2 answers

factoryboy not working with freezegun

simple model(models.py): from django.db import models class MyModel(models.Model): start_date = models.DateField() simple factory(test_factories.py): from datetime import date import factory from .models import MyModel class…
Pyae
  • 500
  • 1
  • 4
  • 15
5
votes
1 answer

freeze_time not working for default param

I am having a function with the a default value of a param as datetime.now(). method is something like below, def as_standard_format(p_date=datetime.now(), fmt=sdk_constants.DEFAULT_DATE_TIME_FORMAT): return p_date.strftime(fmt) And I am having…
Kannan Ramamoorthy
  • 3,980
  • 9
  • 45
  • 63
4
votes
1 answer

How to use Ignore packages in freezegun?

While I use freezegun with google storage api, I got the following error. google.auth.exceptions.RefreshError: ('invalid_grant: Invalid JWT: Token must be a short-lived token (60 minutes) and in a reasonable timeframe. Check your iat and exp values…
lucemia
  • 6,349
  • 5
  • 42
  • 75
4
votes
2 answers

How to pause pytest timer?

I have a parametrized (py)test that cleans up some table in the DB before running the logic: @pytest.mark.parametrize('limit', [1, 1000, 5000000]) def test_calc(limit): # clean test table in database !!! assert calc(limit) == None At the…
EvgenyKolyakov
  • 3,310
  • 2
  • 21
  • 31
4
votes
1 answer

Using Boto3 with freezegun results in 403

I'm using boto3 to load some file from an AWS S3 bucket. Which is working fine. However for my unittests i'm calling freeze_time and then the function returns the error: botocore.exceptions.ClientError: An error occurred (403) when calling the…
Johannes
  • 41
  • 3
4
votes
0 answers

Python Freezegun giving different values when freezing time to datetime.datetime.now()

Im trying to use freezegun to set the clock back 10 seconds for a unit test. I've found that setting the time to now with freezegun results in the expected behavior for datetime.datetime.now() but somewhat different behavior for time.time() in that…
jamis0n
  • 3,610
  • 8
  • 34
  • 50
3
votes
1 answer

Freezegun's freeze_time throws odd transformers error when used

I have a test that seems to throw an odd error when freeze_time is called. Reading the stacktrace, it's most likely interacting with another dependency in an odd way, but I don't have enough experience with python to understand what the problem may…
rbhalla
  • 869
  • 8
  • 32
3
votes
1 answer

Why doesn't FreezeGun work with SQLAlchemy default values?

I created a SQLAlchemy app with the following model: class MyObject(db.Model): __tablename__ = 'my_object' id = db.Column(db.Integer, nullable=False, primary_key=True, autoincrement=True) some_string = db.Column(db.String(20),…
Saqib Ali
  • 11,931
  • 41
  • 133
  • 272
3
votes
1 answer

Pytest flask live_server: can't freeze time

I'm trying to implement unit tests for a Flask application using pytest-flask. The output of my queries is dependent on the current time. For consistent unit tests, I'm trying to freeze the time. I'm used to freezegun so here is what I tried: #…
JPFrancoia
  • 4,866
  • 10
  • 43
  • 73
3
votes
1 answer

Freezegun always causes RuntimeWarning of receiving naive datetime

I'm working with a testcase where an organization can be suspended. Currently I'm using freezegun to freeze a firm time, which is a datetime.datetime object with tzinfo=pytz.UTC. In the test below you'll see a print of self.fake_datetime which…
Nrzonline
  • 1,600
  • 2
  • 18
  • 37
3
votes
0 answers

Django Unit test : `freeze_time` not work well in some cases (Seems like a bug..?)

This is my code for testing the role of TIME_ZONE and `USE_TZ: from django.test.utils import override_settings class TimeZoneTest(TestCase): @override_settings(TIME_ZONE='UTC', USE_TZ=True) @freeze_time("2018-01-04 13:30:55", tz_offset=9)…
user3595632
  • 5,380
  • 10
  • 55
  • 111
1
2