Learn to use Pytest

Jesús
3 min readNov 11, 2023

Pytest has evolved into a popular and powerful testing framework, offering developers an intuitive and efficient way to validate the functionality of their code.

Understanding Pytest can significantly elevate your testing practices.

In this story, we will delve into the key aspects of Pytest, exploring its installation, writing and organizing tests and more.

Installation

You can install it with pip:

pip install -U pytest

Basic Structure of a Pytest Test Module

Naming Convection

Pytest identifies test modules and functions based on naming conventions. Test modules should start with test_, and test functions should also start with test_ .

Writing Test Functions

Define your test functions within the module. These functions should test specific units of code or functionality.

# Example of a Pytest test function
def test_addition():
assert 1 + 1 == 2

It’s important to use the assert statement to check if the expression is true. If it's not true, Pytest will raise an assertion error.

Running Pytest

--

--

Jesús

I am an engineer and I love doing different projects in different programming languages, being Python and Excel the languages I use the most