我正在使用WTForms。当我运行我的测试时,我收到以下错误:
RuntimeError:在应用程序上下文之外工作
这是我测试的简化版本:
from unittest import TestCase
from forms.my_form import MyForm
class MyFormTest(TestCase):
def test_expects_to_instantiate_new_form(self):
form = MyForm()
self.assertIsInstance(form, MyForm)
我的表单的简化版本:
from flask_wtf import FlaskForm
from wtforms import HiddenField
from wtforms.validators import Required
class MyForm(FlaskForm):
hidden_field = HiddenField('Label will not be visible', [Required()])
答案 0 :(得分:1)
要提供所需的应用程序上下文,我manually pushed a context:
from unittest import TestCase
from forms.my_form import MyForm
from flask import Flask
class MyFormTest(TestCase):
def test_expects_to_instantiate_new_form(self):
# Arrange
app = Flask(__name__)
# Act
with app.test_request_context('/'):
form = MyForm()
# Assert
self.assertIsInstance(form, MyForm)