简体   繁体   English

烧瓶。 应用工厂测试

[英]Flask. Application Factory Testing

I am trying this "Application Factory" stuff in Flask.我正在 Flask 中尝试这个“应用程序工厂”的东西。 When I run the application via flask, everything works fine (as expected).当我通过烧瓶运行应用程序时,一切正常(如预期)。

#__init__.py
from flask import Flask
from .config import config_by_name
from flask import Flask

def create_app(config_name):
    app = Flask(__name__)
    app.config.from_object(config_by_name[config_name])
    return app



#config.py
class Config:
    SECRET_KEY = os.getenv('SECRET_KEY', 'my_precious_secret_key')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    DEBUG = False

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = "postgresql://postgres@10.11.12.13:1234/DB"
    SECRET_KEY = 'dev!'

class TestingConfig(Config):
    DEBUG = True
    TESTING = True
    PRESERVE_CONTEXT_ON_EXCEPTION = False
    SQLALCHEMY_DATABASE_URI = 'sqlite:///testing.db'
    SECRET_KEY = 'test!'

config_by_name = dict(
    dev=DevelopmentConfig,
    test=TestingConfig
)

key = Config.SECRET_KEY



#app.py
from flask import Flask
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

from application import create_app
app = create_app('dev')

engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
DBSession = sessionmaker(bind=engine)
session = DBSession()

@app.route('/item/<int:id>', methods=['GET', 'POST'])
def do_something(id):
    return '%s' % session.bind

if __name__ == '__main__':
    app.run()

When I call http://localhost:5000/item/1 I get the expected result: Engine(postgresql://postgres@10.11.12.13:1234/DB)当我调用http://localhost:5000/item/1 时,我得到了预期的结果: Engine(postgresql://postgres@10.11.12.13:1234/DB)

Now I want to write some tests.现在我想写一些测试。 How can I switch to the "testing config"?如何切换到“测试配置”?

# test_app.py
import unittest
from application.app import app
from flask_testing import TestCase
from sqlalchemy.orm import sessionmaker
from sqlalchemy import create_engine

class BaseTestCase(TestCase):
    def create_app(self):
        app.config.from_object('application.config.TestingConfig')
        return app

    def setUp(self):
        engine = create_engine(app.config['SQLALCHEMY_DATABASE_URI'])
        DBSession = sessionmaker(bind=engine)
        self.session = DBSession()

    def tearDown(self):
        pass

    def test_edit(self):
        response = self.client.get('/item/9999')
        print(response.data)

if __name__ == '__main__':
    unittest.main()

When I run the unittests I get the also the "dev" DB-Connection:当我运行单元测试时,我也得到了“开发”数据库连接:

python -m unittest application/test_app.py
b'Engine(postgresql://postgres@10.11.12.13:1234/DB)'

How do I get the "sqlite:///testing.db" connection.如何获得“sqlite:///testing.db”连接。 What am I doing wrong?!我究竟做错了什么?!

The main problem is that you are trying to use 'created app' in tests.主要问题是您试图在测试中使用“创建的应用程序”。 Look at test.py :看看test.py

from application.app import app

You imported configured app.您导入了配置的应用程序。 What doesn't mean?不是什么意思? The following code was executed:执行了以下代码:

app = create_app('dev')

After that you try to update configured app in tests but it doesn't work:之后,您尝试在测试中更新配置的应用程序,但它不起作用:

def create_app(self):
    app.config.from_object('application.config.TestingConfig')

The easiest way is just use ENV variable for configuration:最简单的方法是使用ENV变量进行配置:

def create_app(config_name):
    app = Flask(__name__)
    # get config name from env variable
    config_name = os.environ.get('FLASK_CONFIG', config_name)
    app.config.from_object(config_by_name[config_name])
    return app

From terminal:从终端:

export FLASK_CONFIG=test
# you can run app with 'test' config python application/app.py
# or run tests etc...
nosetests ./tests/ --exe --logging-level=INFO

Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM