A Beginner’s Guide to pytest

Posted by

Introduction to pytest: A Beginner’s Guide

Welcome to our beginner’s guide to pytest!

Pytest is a powerful and easy-to-use test framework for Python. In this tutorial, we will cover the basics of pytest and show you how to get started with writing tests for your Python code. Let’s dive in!

Installing pytest

Before we can start writing tests with pytest, we need to install it. You can install pytest using pip, the Python package manager. Just run the following command in your terminal:

pip install pytest

Writing your first test

Now that pytest is installed, let’s write our first test. Create a new Python file, for example test_example.py, and add the following code:

def add_numbers(a, b):
    return a + b

def test_add_numbers():
    assert add_numbers(1, 2) == 3
    assert add_numbers(10, -5) == 5
    assert add_numbers(-1, -1) == -2

In this test, we define a simple function add_numbers that adds two numbers together, and then we define a test function test_add_numbers that checks that the function works correctly. To run the test, open a terminal and run the following command:

pytest test_example.py

If all the assertions pass, you should see an output like this:

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

test_example.py .                                              [100%]

========================= 1 passed in 0.01 seconds ========================

Writing more complex tests

pytest supports a wide variety of assertions and features to make testing your code easier. You can use fixtures to set up common test data, use parameters to run the same test with different inputs, and use markers to categorize and select tests.

Here’s an example of using a fixture to set up test data:

import pytest

@pytest.fixture
def my_data():
    return {'name': 'Alice', 'age': 30}

def test_name(my_data):
    assert my_data['name'] == 'Alice'

In this test, we define a fixture my_data that returns a dictionary with a name and age key. The test function test_name accepts the fixture as an argument and checks that the name key is equal to ‘Alice’.

Conclusion

Congratulations! You’ve now learned the basics of pytest and how to write tests for your Python code. Remember, writing tests is an important part of software development as it helps ensure that your code works as intended and catches any bugs early on. Keep practicing and exploring more features of pytest to become a testing pro!

0 0 votes
Article Rating
4 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@davemcdonald6799
1 month ago

I found this one of the better introductory pytest videos, nice job! I'll be sharing this video with others on my team that also need to come up to speed on pytest.

@freedomsarge
1 month ago

Jose – as always great quality and explanation! 🙂

@mfawzi89
1 month ago

Sound funny sometimes..
I have to write a code to test the code I wrote 😂
Thank you Jose👍🏻

@gustavomatto9737
1 month ago

Time waiting for this, Jose, thanks a lot! Are you thinking about a Udemy course with API testing and Pytest? I guess the FastApi will cover that, doesn't it? Currently I'm working with Playwright as framework, did you tried it?