Open In App

File Execution in Pytest

In this article, we will explore how to execute test files in Pytest, along with various options and techniques to customize test runs.

What is Pytest?

Pytest is a Python-based testing framework designed for creating and running test codes. While it excels in API testing in the current landscape of REST services, Pytest is versatile enough to handle a wide range of tests, from basic to complex. This includes testing APIs, databases, user interfaces, and more.



Pytest Features

Install Pytest in Python

We can install Pytest by writing the following command in the command prompt:

pip install pytest

Creating Test Files

First, create a test file which will be tested named ‘my_functions.py’.






def add_numbers(x, y):
    return x + y
 
def subtract_numbers(x, y):
    return x - y
 
def multiply_numbers(x, y):
    return x * y
 
def divide_numbers(x, y):
    if y == 0:
        raise ValueError("Cannot divide by zero")
    return x / y

Writing a Test Function

Create a another new file named ‘test_my_functions.py’. Pytest automatically identifies files with this naming convention as containing tests.

In test_my_functions.py write a test function using the pytest syntax.




import my_functions
 
def test_addition():
    result = my_functions.add_numbers(2, 3)
    assert result == 5, f"Expected 5, but got {result}"
 
def test_subtraction():
    result = my_functions.subtract_numbers(5, 2)
    assert result == 3, f"Expected 3, but got {result}"
 
def test_multiplication():
    result = my_functions.multiply_numbers(3, 4)
    assert result == 12, f"Expected 12, but got {result}"
 
def test_division():
    result = my_functions.divide_numbers(10, 2)
    assert result == 5, f"Expected 5, but got {result}"
 
    # Additional test for division by zero
    try:
        my_functions.divide_numbers(10, 0)
    except ValueError as e:
        assert str(e) == "Cannot divide by zero", f"Expected 'Cannot divide by zero', but got '{e}'"
 
def test_addition_negative():
    result = my_functions.add_numbers(-2, 3)
    assert result == 1, f"Expected 1, but got {result}"

Output

test_my_functions Output

Handling Test Failure Example

When a test fails in Pytest, it means that the expected outcome did not match the actual outcome. Pytest provides clear feedback to identify the cause of the failure. Understanding these techniques will help you efficiently manage and execute your test suites, ensuring the reliability and quality of your Python applications.

Let’s understand, What If a test fails:

Example

Create a file named my_functions.py with the following content:




def add_numbers(x, y):
    return x + y

Create a file named test_my_functions.py with the following content:




from my_functions import add_numbers
 
def test_addition():
    result = add_numbers(2, 3)
    # This assertion will fail
    assert result == 6 

Output:

Assertion Error Output

In this example, pytest clearly indicates that the test_addition function failed, and it shows that the assertion assert result == 6 was not met because 5 != 6. This feedback helps you quickly identify what went wrong and where.


Article Tags :