简体   繁体   中英

Freezetime does not work with FastAPI test client

Freezetime does not seem to work with FastAPI TestClient. I have build this simple example, the test is failing. Freezetime does not override datetime in this case:/

import datetime

from fastapi import FastAPI
from fastapi.testclient import TestClient
from pydantic import BaseModel
from freezegun import freeze_time

app = FastAPI()


class Message(BaseModel):
    message: str = "Hello World"
    timestamp: datetime.datetime = datetime.datetime.utcnow()


@app.get("/", response_model=Message)
def main() -> Message:
    return Message()


client = TestClient(app)

@freeze_time('2022-09-18T13:36:41.624237')
def test_read_main():
    response = client.get("/")
    assert response.status_code == 200
    assert response.json() == {
            'message': 'Hello World',
            'timestamp': '2022-09-18T13:36:41.624237'
            }

when I run pytest I am getting this message

    @freeze_time('2022-09-18T13:36:41.624237')
    def test_read_main():
        response = client.get("/")
        assert response.status_code == 200
>       assert response.json() == {
                'message': 'Hello World',
                'timestamp': '2022-09-18T13:36:41.624237'
                }
E       AssertionError: assert {'message': '...44:25.021208'} == {'message': '...36:41.624237'}
E         Omitting 1 identical items, use -vv to show
E         Differing items:
E         {'timestamp': '2022-09-18T13:44:25.021208'} != {'timestamp': '2022-09-18T13:36:41.624237'}
E         Use -v to get more diff

Any ideas if these kind of tests are possible with the FastAPI TestClient?

As mentioned here , define another function and assign the reference of that function to the model. That way, the freezegun should be able to override the datetime.

...
from pydantic import Field

def current_time():
    return datetime.datetime.utcnow()

class Message(BaseModel):
    message: str = "Hello World"
    timestamp: datetime.datetime = Field(default_factory=current_time)
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM