如何测试自定义Flask错误页面?

时间:2014-08-18 19:38:27

标签: python flask

我试图在烧瓶中测试自定义错误页面(在这种情况下为404)。

我已经定义了我的自定义404页面:

@app.errorhandler(404)
def page_not_found(e):
    print "Custom 404!"
    return render_template('404.html'), 404

这在浏览器中点击未知页面时效果很好(我在stdout中看到Custom 404!并且我的自定义内容可见)。但是,当尝试通过unittest nose触发404时,标准/服务器404页面呈现。我没有收到日志消息或我试图测试的自定义内容。

我的测试用例定义如下:

class MyTestCase(TestCase):
    def setUp(self):
        self.app = create_app()
        self.app_context = self.app.app_context()
        self.app.config.from_object('config.TestConfiguration')
        self.app.debug = False # being explicit to debug what's going on...
        self.app_context.push()
        self.client = self.app.test_client()

    def tearDown(self):
        self.app_context.pop()

    def test_custom_404(self):
        path = '/non_existent_endpoint'
        response = self.client.get(path)
        self.assertEqual(response.status_code, 404)
        self.assertIn(path, response.data)

我的app.debug已在我的测试应用中明确设置为False。还有什么我必须明确设置吗?

3 个答案:

答案 0 :(得分:5)

用新眼睛重新审视之后,显然问题出在我的应用程序初始化而不是我的测试/配置中。我的应用__init__.py基本上看起来像这样:

def create_app():
    app = Flask(__name__)
    app.config.from_object('config.BaseConfiguration')
    app.secret_key = app.config.get('SECRET_KEY')
    app.register_blueprint(main.bp)
    return app

app = create_app()

# Custom error pages

@app.errorhandler(404)
def page_not_found(e):
    return render_template('404.html'), 404

请注意,错误处理程序已附加到@app之外的create_app(),这是我在TestCase.setUp()方法中调用的方法。

如果我只是将错误处理程序移到create_app()方法中,一切正常......但感觉有点粗糙?可能?

def create_app():
    app = Flask(__name__)
    app.config.from_object('config.BaseConfiguration')
    app.secret_key = app.config.get('SECRET_KEY')
    app.register_blueprint(main.bp)

    # Custom error pages
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('404.html'), 404

    return app

这最终回答了我的问题并解决了我的问题,但我还是喜欢关于如何不同地注册这些错误处理程序的其他想法。

答案 1 :(得分:1)

Flask 应用程序对象有一个 error_handler_spec 属性,可以通过模拟来解决这个问题:

<块引用>

所有已注册错误处理程序的字典。关键是 None 为 应用程序上活动的错误处理程序,否则关键是 蓝图的名称。每个键都指向另一个字典,其中 key是http异常的状态码。特殊键 无 指向元组列表,其中第一项是 实例检查和第二个错误处理函数。

所以在你的测试方法中应该有这样的东西:

mock_page_not_found = mock.magicMock()  
mock_page_not_found.return_value = {}, 404

with mock.patch.dict(self.app.error_handler_spec[None], {404: mock_page_not_found}):
  path = '/non_existent_endpoint'
  response = self.client.get(path)

  self.assertEqual(response.status_code, 404)
  mock_page_not_found.assert_called_once()

答案 2 :(得分:0)

参考您提出的以下评论“如果我只是将该错误处理程序向上移动到 create_app() 方法中,则一切正常……但感觉有点恶心?也许?”: 您可以定义一个函数来注册错误处理程序并在 create_app 函数中调用它:

def create_app():
    app = Flask(__name__)
    app.config.from_object('config.BaseConfiguration')
    app.secret_key = app.config.get('SECRET_KEY')
    app.register_blueprint(main.bp)
    register_error_pages(app)
    return app

app = create_app()

# Custom error pages

def register_error_pages(app):
    @app.errorhandler(404)
    def page_not_found(e):
        return render_template('404.html'), 404

这样,如果您要注册更多自定义错误处理程序(403、405、500),您可以在 register_error_pages 函数中定义它们,而不是在 create_app 函数中定义它们。