[英]Flask app.config during unit testing
處理依賴於調用代碼的單元測試的最佳方法是什么,而代碼又依賴於當前應用程序的配置?
例如
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
class TestCode(unittest.TestCase):
def test_some_method(self):
app = create_app('app.settings.TestConfig')
value = some_method()
self.assertEqual(10, value)
運行上面的測試,當執行app = create_app('app.settings.TestConfig')行時,我得到'RuntimeError:在應用程序上下文之外工作'錯誤。
在測試期間調用app = create_app並不能解決問題。 在我需要在應用程序中讀取配置的情況下,單元測試的最佳方法是什么?
當您調用some_method()來修復它時,您正在使用應用程序上下文中的應用程序來替換您的呼叫:
with app.app_context():
value = some_method()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.