pytest-flask example with SQLAlchemy
@northernSage 已经在做这个了。
开始于 2021年3月25日。
评估
这个 Issue 还没有评估数据。
描述
Hi, there
Could you include a SQLAlchemy example in the documentation? I'm using the following code but it doesn't work:
import pytest
from projeto import create_app, db as _db
@pytest.fixture(scope='session')
def app():
app = create_app('testing')
return app
@pytest.fixture(scope='session')
def db(app, request):
_db.app = app
_db.create_all()
yield _db
_db.drop_all()
@pytest.yield_fixture(scope='function')
def session(db):
connection = db.engine.connect()
transaction = connection.begin()
options = dict(bind=connection)
session = db.create_scoped_session(options=options)
db.session = session
yield session
transaction.rollback()
connection.close()
session.remove()
This is the error that I get:
app = <Flask 'projeto'>, request = <SubRequest 'db' for <Function 'test_login_page'>>
@pytest.fixture(scope='session')
def db(app, request):
_db.app = app
_db.create_all()
yield _db
> _db.drop_all()
conftest.py:14:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:1015: in drop_all
self._execute_for_all_tables(app, bind, 'drop_all')
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:999: in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:941: in get_engine
return connector.get_engine()
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:533: in get_engine
uri = self.get_uri()
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:524: in get_uri
return self._app.config['SQLALCHEMY_DATABASE_URI']
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/werkzeug/local.py:347: in __getattr__
return getattr(self._get_current_object(), name)
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/werkzeug/local.py:306: in _get_current_object
return self.__local()
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
def _find_app():
top = _app_ctx_stack.top
if top is None:
> raise RuntimeError(_app_ctx_err_msg)
E RuntimeError: Working outside of application context.
E
E This typically means that you attempted to use functionality that needed
E to interface with the current application object in a way. To solve
E this set up an application context with app.app_context(). See the
E documentation for more information.
/home/andref/.virtualenvs/projeto/lib/python3.5/site-packages/flask/globals.py:51: RuntimeError
- 主要语言
- Python
- 星标
- 499
- 派生
- 93
- PR 合并指标
- 30 天内没有已合并 PR
环境准备
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
pytest-dev/pytest-flask 的其他 Issue
-
bug
难度 3/5 1-2 天 新手友好度 35/100
pytest-dev/pytest-flask#190 · 1 条评论 · 1 个 reaction ·
-
bug
难度 4/5 3-5 天 新手友好度 35/100
pytest-dev/pytest-flask#187 · 7 条评论 ·
-
bug
难度 2/5 1-3 小时 新手友好度 45/100
pytest-dev/pytest-flask#175 · 3 条评论 ·
-
难度 4/5 3-5 天 新手友好度 35/100
pytest-dev/pytest-flask#163 · 1 条评论 · 1 个 reaction ·
-
feature request
难度 4/5 3-5 天 新手友好度 45/100
pytest-dev/pytest-flask#153 · 1 条评论 ·
查看 pytest-dev/pytest-flask 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 72/100
-
bug
难度 1/5 1 小时以内 新手友好度 88/100
qgis/QGIS-Plugins-Website#459 ·
-
bug severity:medium
难度 2/5 1-3 小时 新手友好度 78/100
维护者通常 2 天内回复
-
bot-found bug priority: P3
难度 2/5 1-3 小时 新手友好度 84/100
madenvel/KalinkaPlayer#179 ·
-
难度 2/5 1-3 小时 新手友好度 68/100
ls1intum/edutelligence#1098 ·
维护者通常 1 天内回复