我正在尝试为 Flask 设置
PyTest
并遇到错误 TypeError:
unsupported operand type(s) for -: 'Flask' and 'Flask'
我的
conftest.py
文件中有两个灯具:
from application import init_app, db
@pytest.fixture(scope="module")
def test_app():
"""fixture for testing the application"""
app - init_app()
app.config.from_object("app.config.TestingConfig")
with app.app_context():
yield app
@pytest.fixture(scope="module")
def test_db():
"""fixture for cleaning and initializing the database"""
db.create_all()
yield db
db.session.remove()
db.drop_all()
然后我有几个测试只是测试我的配置文件:
"""Tests the flask app configuration app.config.py"""
def test_config(test_app):
test_app.config.from_object("app.config.Config")
assert test_app.config["JWT_SECRET_KEY"] == "Super_Secret"
assert not test_app.config["TESTING"]
assert test_app.config["SQLALCHEMY_DATABASE_URI"] == "mysql+pymysql://root:"
def test_testing_config(test_app):
test_app.config.from_object("app.config.TestingConfig")
assert test_app.config["SQLALCHEMY_DATABASE_URI"] == "mysql+pymysql://root:"
assert test_app.config["TESTING"]
对于上下文,我导入了我的 init_app 所以这里是:
def init_app():
app = Flask(__name__, instance_relative_config=False)
app.config.from_object(Config)
# Setup plugins
db.init_app(app)
ma.init_app(app)
bcrypt.init_app(app)
JWTManager.init_app(app)
migration.init_app(app, db)
mail.init_app(app)
cors.init_app(app)
from application.models import Answers
with app.app_context():
# Blueprints
from application.answers import answers_blueprint
app.register_blueprint(answers_blueprint)
return app
app - init_app()
有错字:
@pytest.fixture(scope="module")
def test_app():
"""fixture for testing the application"""
# app - init_app() # Typo here, should be = instead of -
app = init_app()
app.config.from_object("app.config.TestingConfig")
with app.app_context():
yield app