Use pytest-dotenv for test environment #1363
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The
runtests.sh
script that we use to create a test environment is not compatible with Visual Studio Code, which expects to be able to callpytest
directly.This PR moves test environment initialization directly into pytest, using the pytest-dotenv plugin. To use:
Move existing secrets file from
secrets.test
to.env.testing
. If you don't have an existingsecrets.test
file, bootstrap by copying.env.testing-sample
to.env.testing
and edit as needed.This file must include
FLASK_ENV=testing
, to tell Flask and Funnel to run in a test environment. (However, this does not appear to work, see below).Ensure pytest-dotenv is installed.
Continue using the
runtests.sh
script as usual, or useFLASK_ENV=testing pytest
Flask will use python-dotenv if it is installed (a dependency for pytest-dotenv) and will automatically load from files named
.env
and.flaskenv
. However, this appears to happen when the server is run, or when the tests are run, not when the app is loaded. SinceFLASK_ENV=testing
must be set at load time, it appears to be ineffective for choosing the runtime environment andruntests.sh
is still necessary. However, VS Code appears to be setting FLASK_ENV correctly when running tests inside the IDE.Running
pytest
as is currently causes this exception: