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 …


This content originally appeared on DEV Community and was authored by Gábor Szabó

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!'


This content originally appeared on DEV Community and was authored by Gábor Szabó


Print Share Comment Cite Upload Translate Updates
APA

Gábor Szabó | Sciencx (2025-08-13T13:14:00+00:00) Flask: testing hello world. Retrieved from https://www.scien.cx/2025/08/13/flask-testing-hello-world/

MLA
" » Flask: testing hello world." Gábor Szabó | Sciencx - Wednesday August 13, 2025, https://www.scien.cx/2025/08/13/flask-testing-hello-world/
HARVARD
Gábor Szabó | Sciencx Wednesday August 13, 2025 » Flask: testing hello world., viewed ,<https://www.scien.cx/2025/08/13/flask-testing-hello-world/>
VANCOUVER
Gábor Szabó | Sciencx - » Flask: testing hello world. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/08/13/flask-testing-hello-world/
CHICAGO
" » Flask: testing hello world." Gábor Szabó | Sciencx - Accessed . https://www.scien.cx/2025/08/13/flask-testing-hello-world/
IEEE
" » Flask: testing hello world." Gábor Szabó | Sciencx [Online]. Available: https://www.scien.cx/2025/08/13/flask-testing-hello-world/. [Accessed: ]
rf:citation
» Flask: testing hello world | Gábor Szabó | Sciencx | https://www.scien.cx/2025/08/13/flask-testing-hello-world/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.