简体   繁体   English

在单元测试期间烧瓶app.config

[英]Flask app.config during unit testing

What is the best way handle unit tests that rely on calling code that in turn relies on the current app's configuration? 处理依赖于调用代码的单元测试的最佳方法是什么,而代码又依赖于当前应用程序的配置?

eg 例如

code.py code.py

from flask import current_app

def some_method():
    app = current_app._get_current_object()
    value = (app.config['APP_STATIC_VAR'])*10
    return value

test_code.py test_code.py

class TestCode(unittest.TestCase):
    def test_some_method(self):
        app = create_app('app.settings.TestConfig')
        value = some_method()
        self.assertEqual(10, value)

Running the test above I get an 'RuntimeError: working outside of application context' error when the app = create_app('app.settings.TestConfig') line is executed. 运行上面的测试,当执行app = create_app('app.settings.TestConfig')行时,我得到'RuntimeError:在应用程序上下文之外工作'错误。

Calling app = create_app during the test doesn't do the trick. 在测试期间调用app = create_app并不能解决问题。 What is the best way to unit test in this case where I am needing the config to be read in the the application? 在我需要在应用程序中读取配置的情况下,单元测试的最佳方法是什么?

You are using accessing the app within an app context when you call some_method() to fix it replace your call with: 当您调用some_method()来修复它时,您正在使用应用程序上下文中的应用程序来替换您的呼叫:

with app.app_context():
    value = some_method()

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

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