Create Parametrize Pytest Test for a Function in Python

In this guide, you’ll see how to create a parametrize Pytest test for a given function in Python.

To start, here is one simple template to create a parametrize Pytest test. In the next section, you’ll see how to apply this template using a practical example.

import pytest

# Import the function you want to test
from your_module_name import your_function_name

# Define the test cases using pytest.mark.parametrize
@pytest.mark.parametrize("input_value, expected_output", [
    (input_data_1, expected_output_1),
    (input_data_2, expected_output_2),
    (input_data_3, expected_output_3),
    ...
])
def test_your_function_name(input_value, expected_output):
    result = your_function_name(input_value)
    assert result == expected_output

Steps to Create Parametrize Pytest Test for a Function in Python

Step 1: Install the Pytest package

If you haven’t already done so, install the Pytest package using the following command:

pip install pytest

Step 2: Write your Python script

Write your python script with the function you wish to test.

For example, the following Python function takes an input sentence and replaces the word “world” with “universe“:

def replace_world_with_universe(sentence: str) -> str:
    """
    Replace the word 'world' for 'universe' in a given sentence.

    Args:
        sentence (str): The input sentence to process.

    Returns:
        str: The modified sentence where 'world' replaced by 'universe'.
    """
    modified_sentence = sentence.replace('world', 'universe')
    return modified_sentence

Save the above function in a Python file.

For our example, let’s save the function in a Python file called “replacing_words_module”. Note that the file should be saved with .py file extension.

Step 3: Write the parametrize Pytest test

In a new Python file, write the parametrize Pytest test.

For our example:

import pytest

from replacing_words_module import replace_world_with_universe

@pytest.mark.parametrize("input_sentence, expected_output", [
    ("Hello world, this is indeed a new world", "Hello universe, this is indeed a new universe"),
    ("The world is vast and the world is beautiful", "The universe is vast and the universe is beautiful"),
    ("No change in this sentence.", "No change in this sentence."),
    ("world", "universe"),
    ("", ""),
    ("This is a test that has no 'world'", "This is a test that has no 'universe'"),
    ("Worlds collide in a complex world", "Worlds collide in a complex universe")
])
def test_replace_world_with_universe(input_sentence, expected_output):
    result = replace_world_with_universe(input_sentence)
    assert result == expected_output

Save the above parametrize Pytest test in a new Python file called “test_replacing_words”. Note that you should save the Python file with a “test_” prefix for common convention. Also, make sure to save the file with .py file extension.

Step 4: Run the Pytest test

Go to the terminal, and navigate to the directory where the test file is located.

Then run the following command to run pytest:

pytest test_replacing_words.py

Alternatively, you can run the “pytest .” command (in the terminal) to run all the test files located in the current directory and its subdirectories:

pytest .

The result:

'[100%] ======================== 7 passed in 0.14s ========================'

For more information, please visit the Pytest Documentation.

Leave a Comment