内存数据库测试中的Flask失败,未找到应用程序。要么在视图功能中工作要么在推动视图功能。

时间:2018-02-13 06:12:33

标签: python python-2.7

我已经在烧瓶中写了我的unitest(在内存数据库中);

import unittest
import json
from lobdcapi import app
from dal.dbContext import db
from models.AtgSiteToPoll import *

class TestAtgSiteToPollEP(unittest.TestCase):
    def setUp(self):
        app.testing = True
        app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///:memory:'
        app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
        self.app = app.test_client()

        with app.app_context():
            db.init_app(app)
            db.drop_all()
            db.create_all()


    def tearDown(self):
        pass

    def test_atg_sitetopoll(self):
        a = AtgSiteToPoll()      
        a.SiteId = 1
        a.IPAddress = '10.10.10.10'
        a.Port = 3000
        a.Category = 1
        a.UserId = 'test'
        a.Password = 'test123'
        a.ReceiveTimeoutInMilliSeconds = 60
        db.session.add(a)
        db.session.commit()

        assert len(AtgSiteToPoll.query.all()) is 1

运行测试时,我得到了;

Traceback (most recent call last):
  File "/Users/ratha/projects/test711/ATGWS/tests/TestAtgSitesToPollEP.py", line 31, in test_atg_sitetopoll
    db.session.add(a)
  File "/Users/ratha/projects/test711/ATGWS/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 153, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/Users/ratha/projects/test711/ATGWS/venv/lib/python2.7/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
    return self.registry.setdefault(key, self.createfunc())
  File "/Users/ratha/projects/test711/ATGWS/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2939, in __call__
    return self.class_(**local_kw)
  File "/Users/ratha/projects/test711/ATGWS/venv/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 141, in __init__
    self.app = app = db.get_app()
  File "/Users/ratha/projects/test711/ATGWS/venv/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 912, in get_app
    'No application found. Either work inside a view function or push'
RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

我在这里做错了什么?

0 个答案:

没有答案