Skip to main content

Running and understanding test results

In this tutorial, we'll explore how to run test cases in Flask and understand their results. Testing is a crucial aspect of software development that ensures your application functions as expected.

Prerequisites

Before we start, ensure you have the following:

  • A basic understanding of the Flask framework.
  • Python installed on your local machine.
  • A text editor, such as VS Code, Sublime Text, or Atom.
  • A virtual environment for Python.

Installing pytest

Pytest is a testing framework that allows us to easily create small, simple tests. To install pytest, activate your virtual environment and use the pip command:

pip install pytest

Writing Your First Test

Let's start with a simple "Hello, World!" app in Flask:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
return 'Hello, World!'

Let's write a test to make sure our hello_world route returns 'Hello, World!'. Create a new file named test_app.py and write the following code:

def test_hello_world():
from app import app
client = app.test_client()

response = client.get('/')
assert response.data == b'Hello, World!'

Here, we're importing our Flask app and creating a test_client object. We then use this to send a GET request to our app and check that the response data is 'Hello, World!'.

Running the Test

To run the test, use the pytest command in the terminal:

pytest

Pytest discovers all the test files (files that start with test_ or end with _test.py), collects the test functions (functions that start with test_), and executes them.

Understanding Test Results

When pytest runs, it will provide an output that states how many tests were collected and how many passed or failed.

============================= test session starts ==============================
collected 1 item

test_app.py . [100%]

============================== 1 passed in 0.12s ===============================

If the test passes, you'll see a dot (.) and a message indicating the number of tests that passed.

A failed test will show an 'F', along with a traceback and an error message explaining why the test failed.

============================= test session starts ==============================
collected 1 item

test_app.py F [100%]

=================================== FAILURES ===================================
_______________________________ test_hello_world ________________________________

def test_hello_world():
from app import app
client = app.test_client()

response = client.get('/')
> assert response.data == b'Hello, World!'
E AssertionError: assert b'Hello, Flask!' == b'Hello, World!'
E At index 0 diff: 70 != 87
E Use -v to get the full diff

test_app.py:7: AssertionError
=========================== short test summary info ============================
FAILED test_app.py::test_hello_world - AssertionError: assert b'Hello, Flask!'...
============================== 1 failed in 0.12s ===============================

In the above example, the test failed because the returned text was 'Hello, Flask!' and not 'Hello, World!'.

Conclusion

Congratulations! You have successfully run your first Flask test and understood the test results. Remember, writing tests for your application can help catch bugs early and ensure that your program behaves as expected. Happy testing!