Sanic testing, how to load custom env file


I have two questions that are related.

I’m setting up a test suite for my Sanic app, and in some part of the code, I’d need it to behave differently when testing vs when running for true (for instance, when sending emails). How can I test that the current Sanic app is in testing mode ?

The second question is about the environment variables when testing. For the same reason, when testing, I’d like it to use a different database specific for the tests. I assume I need to load some kind of .env.test file, but could you tell me what is the best way to do that?

Thank you in advance!

Checkout dotenv. I use it for this very purpose.

Are you using sanic-testing? If so, then you can check app.test_mode or Sanic.test_mode.

Good catch, that was it! (I’m using sanic-testing indeed).

And in my App loader, by testing if I’m in testing, I can load a custom .env.tests file that has specific property too, so it all resolve to this!