[Python] Create different FastAPI app instances with customized env variable during testing

Discussão em 'Python' iniciado por Stack, Maio 21, 2025.

  1. Stack

    Stack Membro Participativo

    I have a config.py file that initializes a cfg object, which contains all the environmental settings (variables loaded with dotenv from .env file, like DEBUG=FALSE, USING_FASTER_API=TRUE, etc.) that will be used across a FastAPI app.

    Then I have the FastAPI app file app.py that imports the cfg and creates the app:

    app = FastAPI(debug=cfg.DEBUG)


    Also, some endpoints of this app depend on the cfg, for example, say in endpoint /get_feature, it is using an external API that give me some data, but based on the USING_FASTER_API env variable (also imported), it will run a faster version or slower version of the external API.

    I need to test if using fast or slow version of the API will give me same result on the endpoint /get_feature. But how can I create different version of the app (and hence test client) instance by toggling USING_FASTER_API on and off during testing?

    Continue reading...

Compartilhe esta Página