Introduction to pytest

The basics of pytest

Free preview

Resources

Pytest is a powerful testing framework for Python that allows developers to write simple and effective tests for their code. In this article, we will guide you through the process of implementing pytest in a FastAPI project, covering the installation of required packages, writing basic tests, and running them.

Installing our new requirements

Before we start writing tests, we need to install the required packages. In this case, we will be using httpx for making HTTP requests and pytest for writing and running tests. To do this, add the following lines to your requirements-dev.txt file:

storeapi/requirements-dev.txt
httpx
pytest

After updating the file, install the new requirements by running the following command in your terminal:

pip install -r requirements-dev.txt

Writing our first test

Now that we have the required packages installed, let's create a new folder called tests in your project directory. Inside the tests folder, create an empty __init__.py file. This file is necessary for pytest to discover and run the tests inside the tests folder.

Next, create a new file called test_intro.py inside the tests folder. In this file, we will write our first test function called test_add_two. This function will test the addition of two numbers. The code for the test function is as follows:

storeapi/tests/test_intro.py
def test_add_two():
    x = 1
    y = 2
    assert x + y == 3

In this test, we define two variables, x and y, and use the assert statement to check if their sum is equal to 3. If the condition is true, the test will pass; otherwise, it will fail.

How to run our tests

To run the tests, simply execute the pytest command in your terminal. The output should show the test results, indicating whether the tests have passed or failed. In our case, the output should look like this:

storeapi/tests/test_intro.py .
1 passed

The dot (.) in the output represents a successful test, and the "1 passed" message indicates that one test has passed.

Testing dictionary inclusion

In this section, we will write another test function called test_dict_contains. This test will check if one dictionary contains certain values. This is useful when working with JSON data and FastAPI, as Python treats JSON as dictionaries. The test function is as follows:

storeapi/tests/test_intro.py
def test_dict_contains():
    x = {"a": 1, "b": 2}
    assert {"a": 1}.items() <= x.items()

In this test, we define a dictionary x with two key-value pairs. We then use the assert statement to check if the dictionary {"a": 1} is included in the dictionary x. The items() method returns a view of the dictionary's items, and the <= operator checks if all items of the first dictionary are present in the second dictionary. If the condition is true, the test will pass; otherwise, it will fail.

To run the tests again, execute the pytest command in your terminal. The output should now show the results for both tests:

storeapi/tests/test_intro.py ..
2 passed

The two dots (..) in the output represent two successful tests, and the "2 passed" message indicates that both tests have passed.

New files

storeapi/tests/__init__.py

storeapi/tests/test_intro.py
def test_add_two():
    x = 1
    y = 2
    assert x + y == 3


def test_dict_contains():
    x = {"a": 1, "b": 2}
    assert {"a": 1}.items() <= x.items()

Modified files

requirements-dev.txt
--- 
+++ 
@@ -1,3 +1,5 @@
 ruff
 black
 isort
+httpx
+pytest