Flask: testing hello world

flask:-testing-hello-world

Before we go ahead learning how to create more complex web applications we need to learn another very important feature of Flask.

Flask makes it very easy to test your web application without even running a web server.

In order to do this we created a file called test_app.py in the same folder as we have the app.py with the following content.
The name of the files must start with the word test_, but otherwise you can pick any filename.

Inside we import the application using import app and we add a test function. Its name must start with test_, but otherwise we can pick any name.

From the app object we imported, that is, from our Flask application, we can get the test_client which is a representation of our running web application.

Then we can send in various requests. In this case we sent in an HTTP GET request to the root of the site using web.get('/').

We get back a response object that we can then interrogate with various assertions.

To run this we’ll need to install pytest:

pip install pytest

Then we can just type in pytest. It will find and run the tests.

pytest

The test file

import app

def test_app():
    web = app.app.test_client()

    rv = web.get('/')
    assert rv.status == '200 OK'
    assert rv.data == b'Hello World!'
Total
0
Shares
Leave a Reply

Your email address will not be published. Required fields are marked *

Previous Post
how-i-created-an-seo-strategy-that-increased-a-client’s-search-visibility-by-375%

How I Created an SEO Strategy That Increased a Client’s Search Visibility by 375%

Next Post
navigating-product-marketing-in-china

Navigating product marketing in China

Related Posts