测试程序Working outside of application context.
报错
ERROR: test_app_exist (__main__.WatchlistTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_watchlist.py", line 13, in setUp
db.create_all()
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 868, in create_all
self._call_for_binds(bind_key, "create_all")
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 839, in _call_for_binds
engine = self.engines[key]
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 628, in engines
app = current_app._get_current_object() # type: ignore[attr-defined]
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\werkzeug\local.py", line 51
3, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
======================================================================
ERROR: test_app_is_testing (__main__.WatchlistTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
File "test_watchlist.py", line 13, in setUp
db.create_all()
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 868, in create_all
self._call_for_binds(bind_key, "create_all")
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 839, in _call_for_binds
engine = self.engines[key]
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\flask_sqlalchemy\extension.
py", line 628, in engines
app = current_app._get_current_object() # type: ignore[attr-defined]
File "C:\Users\Administrator\anaconda3\envs\python37\lib\site-packages\werkzeug\local.py", line 51
3, in _get_current_object
raise RuntimeError(unbound_message) from None
RuntimeError: Working outside of application context.
This typically means that you attempted to use functionality that needed
the current application. To solve this, set up an application context
with app.app_context(). See the documentation for more information.
----------------------------------------------------------------------
Ran 2 tests in 0.005s
FAILED (errors=2)
原码
import unittest
from app import app, db, Movie, User, forge, initdb
class WatchlistTestCase(unittest.TestCase):
def setUp(self):
app.config.update(
TESTING=True,
SQLALCHEMY_DATABASE_URI='sqlite:///:memory:'
)
db.create_all()
user = User(name='Test', username='test')
user.set_password('123')
movie = Movie(title='Test Movie Title', year='2019')
db.session.add_all([user, movie])
db.session.commit()
self.client = app.test_client()
self.runner = app.test_cli_runner()
def tearDown(self):
db.session.remove()
db.drop_all()
def login(self):
self.client.post('/login', data=dict(
username='test',
password='123'
), follow_redirects=True)
def test_app_exist(self):
self.assertIsNotNone(app)
def test_app_is_testing(self):
self.assertTrue(app.config['TESTING'])
if __name__ == '__main__':
unittest.main()
```python
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,已经为您补发到账户,我们后续会持续优化,扩大我们的服务范围,为您带来更好地服务。