Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I test a custom Flask error page?

Tags:

python

flask

I'm attempting to test a custom error page in flask (404 in this case).

I've defined my custom 404 page as such:

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

This works perfectly when hitting an unknown page in a browser (I see Custom 404! in stdout and my custom content is visible). However, when trying to trigger a 404 via unittest with nose, the standard/server 404 page renders. I get no log message or the custom content I am trying to test for.

My test case is defined like 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 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)

I have app.debug explicitly set to False on my test app. Is there something else I have to explicitly set?

like image 471
broox Avatar asked Aug 18 '14 19:08

broox


People also ask

How do I create a custom error page in Flask?

Flask comes with a handy abort() function that aborts a request with an HTTP error code early. It will also provide a plain black and white error page for you with a basic description, but nothing fancy. Depending on the error code it is less or more likely for the user to actually see such an error.

How do you catch a Flask error?

This can be done by registering error handlers. When Flask catches an exception while handling a request, it is first looked up by code. If no handler is registered for the code, Flask looks up the error by its class hierarchy; the most specific handler is chosen.

How do I create a 404 page Flask?

For this, we need to download and import flask. Download the flask through the following commands on CMD. Using app.py as our Python file to manage templates, 404. html be the file we will return in the case of a 404 error and header.


1 Answers

After revisiting this with fresh eyes, it's obvious that the problem is in my initialization of the application and not in my test/configuration. My app's __init__.py basically looks like 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 that the error handler is attached to @app outside of create_app(), the method I'm calling in my TestCase.setUp() method.

If I simply move that error handler up into the create_app() method, everything works fine... but it feels a 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 ultimately answers my question and fixes my problem, but I'd love other thoughts on how to differently register those errorhandlers.

like image 90
broox Avatar answered Oct 28 '22 20:10

broox