Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use pytest-dotenv for test environment #1363

Merged
merged 3 commits into from
Apr 27, 2022
Merged

Use pytest-dotenv for test environment #1363

merged 3 commits into from
Apr 27, 2022

Conversation

jace
Copy link
Member

@jace jace commented Apr 27, 2022

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 call pytest directly.

This PR moves test environment initialization directly into pytest, using the pytest-dotenv plugin. To use:

  1. Move existing secrets file from secrets.test to .env.testing. If you don't have an existing secrets.test file, bootstrap by copying .env.testing-sample to .env.testing and edit as needed.

  2. 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).

  3. Ensure pytest-dotenv is installed.

  4. Continue using the runtests.sh script as usual, or use FLASK_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. Since FLASK_ENV=testing must be set at load time, it appears to be ineffective for choosing the runtime environment and runtests.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:

tests/unit/transports/test_sms_template.py:24: in <module>
    class MyMessage(SmsTemplate):
funnel/transports/sms/template.py:286: in __init_subclass__
    cls.validate_no_entity_template_id()
funnel/transports/sms/template.py:278: in validate_no_entity_template_id
    raise TypeError(
E   TypeError: Registered entity id and template id are not public information and must be in config. Use init_app to load config

@jace jace requested review from vidya-ram and djamg April 27, 2022 08:59
@jace jace merged commit 327a691 into main Apr 27, 2022
@jace jace deleted the pytest-env branch April 27, 2022 18:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant