How can I check a user page with a Flask error?

I am trying to test a custom error page in a bulb ( 404in this case).

I defined my own 404 page as such:

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

This works great when deleting an unknown page in the browser (I see Custom 404!in stdout and my user content is visible). However, when I try to start 404 through unittestwith nose, the standard / server page 404 is displayed. I do not receive a log message or user content that I am trying to check.

My test case is defined as follows:

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 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.debugexplicitly installed on Falsein my test application. Is there anything else I should explicitly install?

+4
1

, , /. __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

, , - .

+4

All Articles