Comments (7)
Adding to conftest.py does the trick.
from dotenv import load_dotenv
load_dotenv('.flaskenv')
from cookiecutter-flask-restful.
Hello @martgra and thanks for reporting this issue.
This is mainly due to the fact that I haven't updated the readme after swapping to env variables for configuration to specify that you need to setup some variables before running pytest and that I mainly use tox myself and in the CI.
If you look in the tox.ini
you will see that I manually setup env variables here: https://github.com/karec/cookiecutter-flask-restful/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/tox.ini#L22
Be careful using the .flaskenv
for your tests, remember that this is your "local" configuration and tests cleanups delete tables (https://github.com/karec/cookiecutter-flask-restful/blob/master/%7B%7Bcookiecutter.project_name%7D%7D/tests/conftest.py#L29). A simple solution could be to create a test.env
file for exemple.
I will update the readme to reflect this and specify additional steps when using pytest directly
from cookiecutter-flask-restful.
@karec I see. Well I agree that loading .flaskenv might not be a good idea. I still think that when "testing", proper test-configuration should be passed. Perhaps like here:
@pytest.fixture
def app():
db_fd, db_path = tempfile.mkstemp()
app = create_app({
'TESTING': True,
'DATABASE': db_path,
})
yield app
os.close(db_fd)
os.unlink(db_path)
from: https://flask.palletsprojects.com/en/1.1.x/tutorial/tests/
from cookiecutter-flask-restful.
Downside to this solution is when you have different DB based on test env. For example:
- Using SQLite in local env to speed-up tests
- Using Postgres in CI/CD for integration tests to tests specific features (like JSON fields for example)
But we could think about something else, for example:
- Overriding configuration with testing defaults
@pytest.fixture
def app():
app = create_app(testing=True)
app.config["SQLALCHEMY_URL"] = os.getenv("SQLALCHEMY_URL", "sqlite:///:memory:")
yield app
- Using a dedicated config file to override config
test_config.py
import os
SQLALCHEMY_URL = os.getenv("SQLALCHEMY_URL", "sqlite:///:memory:")
app.py
def create_app(testing=False, cli=False):
"""Application factory, used to create application
"""
app = Flask("my_app")
app.config.from_object("my_app.config")
if testing is True:
app.config["TESTING"] = True
app.config.from_object("my_app.test_config")
configure_extensions(app, cli)
configure_apispec(app)
register_blueprints(app)
init_celery(app)
return app
This would allow to set default values for testing without tox or env vars set for local dev but allowing us to override them based on the testing env.
What do you think ?
from cookiecutter-flask-restful.
Personally I prefer the test.env
solution with defaults in conftest.py
.
@pytest.fixture
def app():
load_dotenv("test.env")
app = create_app(testing=True)
app.config["SQLALCHEMY_URL"] = os.getenv("SQLALCHEMY_URL", "sqlite:///:memory:")
yield app
Also we could add a default for CELERY_TASK_ALWAYS_EAGER
here and solve #39.
Thoughts?
from cookiecutter-flask-restful.
This solution is simple and work well, IMHO we could even avoid redefining defaults and rely on the test env file, with a default file inside this cookiecutter and good explanations in the README.
For #39 we can prepare a CELERY_TASK_ALWAYS_EAGER
but this won't solve the issue entirely. To allow tests to run without a rabbitMQ server we should also remove the celery_worker
fixture. But doing so would impact peoples that actually want to test with a broker (inside docker, for CI, etc.), and this should be the default since even celery doc doesn't recommend task_always_eager
for tests: https://docs.celeryproject.org/en/stable/userguide/testing.html#tasks-and-unit-tests
While I agree that this should be an option to simplify local dev, I need to think of a solution that would also let people use a broker to run those tests
from cookiecutter-flask-restful.
I've updated the README and put a .testenv
file as @Lehoczky suggested in d2441a2
I've also updated the README to include more details about testing celery, but we're not yet at the point where we can run celery without a broker out of the box, but let's discuss it on #39
I'm closing this issue since we can now run pytest without needing to set env variables manually, feel free to re-open it if you feel that we still have work to do on that topic
from cookiecutter-flask-restful.
Related Issues (20)
- Separate Schema and Ressources in separates modules HOT 1
- Make the project PEP8 compliant... HOT 2
- Make config class instead of a plain strings HOT 7
- Celery HOT 1
- Celery test failed HOT 4
- add cli elements to flask terminal command HOT 1
- The password inserted into the database when adding a user is a hash value, but it is plaintext when updating a user. HOT 2
- Currently broken because incompatable with Celery 5 HOT 2
- "myapi": executable file not found in $PATH: unknown when using docker HOT 2
- How to debug with pycharm HOT 1
- ImportError: cannot import name 'jwt_refresh_token_required' from 'flask_jwt_extended' HOT 8
- Pagination fails by type error
- tox and pytest HOT 1
- Flask 2.0 just came out and broke all :( HOT 3
- module 'myapi.manage' has no attribute 'cli' HOT 3
- flask db upgrade sqlite fails with import error HOT 2
- Can we support this project by using asgi & async?
- Consider pyproject.toml as an option?
- Use a declarative config?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cookiecutter-flask-restful.