У меня есть фабрика в __init__.py:
Код: Выделить всё
import os
from flask import Flask #For webserver
from flask_sqlalchemy import SQLAlchemy
from flask_jwt_extended import JWTManager
jwt = JWTManager()
db = SQLAlchemy()
def create_app(test_config=None):
app = Flask(__name__, instance_relative_config=True)
if test_config is None:
app.config.from_pyfile('config.py')
try:
os.makedirs(app.instance_path)
except OSError:
pass
@app.route('/')
def hello():
return "Hello world!"
db.init_app(app)
jwt.init_app(app)
with app.app_context():
@app.cli.command('init-db')
def init_db_command():
"""Clear existing data and create new tables."""
init_db()
print('Initialized the database.')
return app
def init_db():
db.create_all()
Когда я запускаю приложение, все работает хорошо. Теперь я хочу написать для этого тест.
Я создал файл conftest.py для определения приборов:
Код: Выделить всё
import os
import tempfile
import pytest
from flaskr import create_app, db, init_db
with open(os.path.join(os.path.dirname(__file__), 'data.sql'), 'rb') as f:
_data_sql = f.read().decode('utf8')
@pytest.fixture
def app():
db_fd, db_path = tempfile.mkstemp(suffix='.db')
app = create_app({
'TESTING' : True,
'SQLALCHEMY_DATABASE_URI ': f'sqlite:///{db_path}',
})
with app.app_context():
init_db()
yield app
db.drop_all()
@pytest.fixture
def client(app):
return app.test_client()
@pytest.fixture
def runner(app):
return app.test_cli_runner()
Код: Выделить всё
from flaskr import create_app
def test_config():
assert not create_app().testing #works
assert create_app({'TESTING': True}).testing # error
def test_hello(client):
response = client.get('/')
assert response.data == b'Hello world' # error
Код: Выделить всё
RuntimeError: Either 'SQLALCHEMY_DATABASE_URI' or 'SQLALCHEMY_BINDS' must be set.
Подробнее здесь: https://stackoverflow.com/questions/787 ... sqlalchemy