Flask/tests/conftest.py

34 lines
844 B
Python
Raw Normal View History

2024-11-10 07:27:36 -08:00
import sys
import pytest
from project_name import create_app
from project_name.ext.commands import populate_db
from project_name.ext.database import db
@pytest.fixture(scope="session")
def app():
app = create_app(FORCE_ENV_FOR_DYNACONF="testing")
with app.app_context():
db.create_all(app=app)
yield app
db.drop_all(app=app)
@pytest.fixture(scope="session")
def products(app):
with app.app_context():
return populate_db()
# each test runs on cwd to its temp dir
@pytest.fixture(autouse=True)
def go_to_tmpdir(request):
# Get the fixture dynamically by its name.
tmpdir = request.getfixturevalue("tmpdir")
# ensure local test created packages can be imported
sys.path.insert(0, str(tmpdir))
# Chdir only for the duration of the test.
with tmpdir.as_cwd():
yield