Getting started with Pytest

Software testing is an integral part of the development cycle of a product. Testing ensures that the product functions smoothly in a user-friendly manner. This article is the first series of articles that demonstrate software testing and its uses and impacts through pytest, a testing tool developed specifically for python.

This post intends to make a small quick start guide that can be used for small testing purposes and get to know pytest rather quickly. We can start by installing pytest.

pip install pytest

Step 1: Creating a simple python function. This is the function we are going to be testing. This can be any function that gives a specific return value or an output of sorts. You can replace this function with the one you are currently working on so that you can get started with pytest right alongside our tutorial.

def add(first: int, second: int) -> int:
 return first + second
 

We have saved this file as “calc.py” . Note that conventionally, the code being tested and the code that does the test must be separated. Ideally, we store the testing code in a folder named “tests”, which contains the testing code for each of the modules we have in different files.

Step 2: Creating a function to test add

from calc import add
def test_add():
    result = add(5, 6)
    assert result == 11`

This is a test upon the function “add” that we created earlier. You can replace it with the respective function and module names if yours are different. This file is a test specifically on the module “calc” and is stored separately in the folder “tests”. It is a convention to have different test files for different modules. We can name this file to be “test_calc.py”. Here, the test only verifies or asserts, that the result of the function add(5,6) is 11. This is not much of a test, as it doesn’t verify the result for the different input types that may go into the “add” function. But this will do for now.

Step 3: Running pytest

You can run the test by using the following command.

pytest test_calc.py

In case there are any difficulties stating there exists no such module, you may run:

export PYTHONPATH="$PYTHONPATH:."
pytest test_calc.py

You will get the following output.

collected 1 item                                 

test_calc.py .                              [100%] 

================ 1 passed in 0.04s ================

The test we wrote for the function “add” did just one thing: it verified that the result of add(5,6) is 11. As such, the “add” function obediently returns the right value for the right kind of input. But this is not strictly the use case of a test. A test is used to evaluate how a function will respond if the inputs given are not desirable or not what the developer had in mind.

Step 4: Checking whether the test fails

A true test upon the “add” function would be something along these lines:

from calc import add
def test_add():
    result = add(5, 6)
    assert result == 11
    assert add("hello","bye") == (False, "This function requires only integral or float arguments")

A real test ensures that there are no inconsistent software failures in any possible conditions that may occur. With such a design in mind, our function, apart from calculating the sum, must also handle such mistakes and unpredictability. Running the test would result in the following:

collected 1 item tests/test_calc.py F                                                   [100%]================== FAILURES ===========================
___________________ test_add ________________________
      def test_add():
        result = add(5,6)
        assert result == 11
>       assert add("hello", "bye") == (False, "This function requires only\
     integral or float arguments")
E       AssertionError: assert 'hellobye' == (False, 'This function requires only integral or float arguments')
E        +  where 'hellobye' = add('hello', 'bye')
tests/test_calc.py:6: AssertionError
================== short test summary info =====================
FAILED tests/test_calc.py::test_add - AssertionError: assert 'hellobye' == (False, 'This func...
===================== 1 failed in 0.52s =========================

We can see that the function “add” clearly cannot handle all kinds of arguments smoothly. This is, of course just one dimension of software testing. We have not provided anyway as to handle such failures and errors, and we will see them in the next article.

As of now, we hope you’ve gained some insight into what software testing actually involves and why it is used as extensively as it is used today. We hope you found this article favorable.

We are, yours truly, Tutorials From Scratch.