spulec / freezegun

Let your Python tests travel through time
Apache License 2.0
4.2k stars 269 forks source link

Decorators for @freeze_time don't work for pytest fixtures #255

Open gbooth27 opened 6 years ago

gbooth27 commented 6 years ago

pytest fixtures decorated with @freeze_time do not have time frozen:

@freeze_time('2018-01-01') @pytest.fixture def sample(): print(datetime.datetime.now(datetime.timezone.utc))

does not freeze the time, this is fixed if a context handler is used inside the fixture

@pytest.fixture def sample(): with freeze_time('2018-01-01'): print(datetime.datetime.now(datetime.timezone.utc))

I am using python 3.5.5 with pytest 3.7.1 and freezegun 0.3.10

jpic commented 6 years ago

Last time it worked was 16 days ago for me.

boxed commented 6 years ago

@gbooth27 The decorator works if you flip the order of the decorators:

@pytest.fixture
@freeze_time('1991-01-01')
def sample():
    return datetime.datetime.now()

def test_pytest_fixture(sample):
    assert datetime.datetime.now() != datetime.datetime(1991, 1, 1)
    assert sample == datetime.datetime(1991, 1, 1)

This works as expected. I suspect there's something wrong with decorate_callable... that it doesn't use functools.wraps seems suspicious to me.