python - How can I test a custom Flask error page? -
i'm attempting test custom error page in flask (404 in case).
i've defined custom 404 page such:
@app.errorhandler(404) def page_not_found(e): print "custom 404!" return render_template('404.html'), 404 this works when hitting unknown page in browser (i see custom 404! in stdout , custom content visible). however, when trying trigger 404 via unittest nose, standard/server 404 page renders. no log message or custom content trying test for.
my test case defined so:
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 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) i have app.debug explicitly set false on test app. there else have explicitly set?
after revisiting fresh eyes, it's obvious problem in initialization of application , not in test/configuration. app's __init__.py looks this:
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 notice error handler attached @app outside of create_app(), method i'm calling in testcase.setup() method.
if move error handler create_app() method, works fine... feels bit gross? maybe?
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 this answers question , fixes problem, i'd love other thoughts on how differently register errorhandlers.
Comments
Post a Comment