Introduction
In this lab, you will learn how to test Flask applications using the pytest framework. Testing is an important part of the software development process, as it helps to ensure the correctness and reliability of your application. Flask provides utilities for testing, making it easy to write tests for different parts of your application.
Note: You need to create the code file yourself and run it in the environment. You can preview the Flask service status on Web 5000.
Set up the Testing Environment
Before you can start writing tests for your Flask application, you need to set up the testing environment. Here are the steps to do that:
Install the
pytestframework by running the following command:pip install pytestCreate a new file called
conftest.pyin thetestsfolder of your Flask application.In the
conftest.pyfile, import the necessary modules:import pytest from my_app import create_appDefine a fixture named
appthat creates and configures an app instance:@pytest.fixture() def app(): app = create_app() app.config.update({ "TESTING": True, }) yield appNote that if you are using an application factory pattern, you should modify the fixture accordingly.
Define fixtures for the test client and CLI runner:
@pytest.fixture() def client(app): return app.test_client() @pytest.fixture() def runner(app): return app.test_cli_runner()
Writing Tests
Now that you have set up the testing environment, you can start writing tests for your Flask application. Here are some examples of common tests you might want to write:
Test a route:
def test_hello(client): response = client.get("/") assert response.status_code == 200 assert b"Hello, World!" in response.dataThis test sends a GET request to the root route ("/") and checks that the response status code is 200 and the response data contains the string "Hello, World!".
Test a POST request:
def test_login(client): response = client.post("/login", data={"username": "test", "password": "pass"}) assert response.status_code == 200 assert b"Logged in successfully" in response.dataThis test sends a POST request to the login route ("/login") with form data containing a username and password. It checks that the response status code is 200 and the response data contains the string "Logged in successfully".
Test a command:
def test_hello_command(runner): result = runner.invoke(args=["hello"]) assert result.exit_code == 0 assert "Hello, World!" in result.outputThis test invokes a command named "hello" and checks that the command exits with a code of 0 and the output contains the string "Hello, World!".
Running the Tests
To run the tests, navigate to the root folder of your Flask application in the terminal and run the following command:
pytest
This command will discover and run all the tests in the tests folder. You should see the test results in the terminal output.
Summary
In this lab, you learned how to test Flask applications using the pytest framework. You set up the testing environment, wrote tests for different parts of your application, and ran the tests to verify the correctness of your code. Testing is an essential part of the software development process, and writing tests for your Flask application will help ensure its reliability and stability.