Git Product home page Git Product logo

Comments (7)

martgra avatar martgra commented on July 17, 2024

Adding to conftest.py does the trick.
from dotenv import load_dotenv
load_dotenv('.flaskenv')

from cookiecutter-flask-restful.

karec avatar karec commented on July 17, 2024

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.

martgra avatar martgra commented on July 17, 2024

@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.

karec avatar karec commented on July 17, 2024

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.

Lehoczky avatar Lehoczky commented on July 17, 2024

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.

karec avatar karec commented on July 17, 2024

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.

karec avatar karec commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.