Open In App

Parameterizing Tests in Pytest

Last Updated : 06 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A testing framework in Python that is used to write API test cases is called Pytest is distinct instances of input and output are possible for a function. In such a case, it is crucial to test the function for all the inputs and outputs. This can be done using the parametrize function in Pytest.

Parameterizing Tests in Pytest

Syntax:

@pytest.mark.parametrize(“input, output”,[(input_1, output_1),(input_2, output_2),(input_3, output_3),(input_4, input_4)])

Here,

  • input, output: These are the parameters to be passed to the function.
  • input_1, input_2, input_3, input_4: These are the input values for which the function needs to be tested.
  • output_1, output_2, output_3, output_4: These are the output values predicted for the specific inputs.

Example 1:

In this example we have created a program, that has two test cases, test_floor and test_square_root. For both of the test cases, we have passed four inputs each to test using parametrize function.

Python3




# Importing the math and Pytest libraries
import math
import pytest
 
# Creating first test case
@pytest.mark.parametrize("input, output", [(5.234, 5), (9.99, 10), (0.456, 0), (7.905, 7)])
def test_floor(input, output):
   assert output==math.floor(input)
 
# Creating second test case
@pytest.mark.parametrize("val, result", [(8, 60), (1, 1), (3, 10), (5, 25)])
def test_square_root(val, result):
   assert result==math.sqrt(val)


Output:

Now, we will run the following command in terminal.

pytest main.py

Example 2:

In this example, we have created a program that has two test cases test_remove_G and test_remove_e. In both the test cases, we have passed two inputs each to test for a specific output.

Python3




# Importing the Pytest library
import pytest
 
# Creating first test case
@pytest.mark.parametrize("input, output",
                         [("Geeks For Geeks",
                           "eeks For eeks"),
                           ("Go Air", "o Air"), ])
def test_remove_G(input, output):
    assert input.replace('G','')==output
 
# Creating second test case
@pytest.mark.parametrize("input, output",
                         [("Geeks For Geeks",
                             "Gaks For Gaks"),
                            ("Engineer", "Enginr"), ])
def test_e(input, output):
    assert input.replace('e','')==output


Output:

Now, we will run the following command in terminal.

pytest main.py

Conclusion

It is necessary for a tester to test each scenario possible, thus he need to test the test case for more than one input. I hope the above article will help you to test your test cases for more than one input, i.e., parametrizing tests. Parametrizing tests is indeed a crucial aspect of software testing. It allows testers to validate the behavior of a piece of software under various input conditions. Here, we will explore the concept of parametrized tests and how to implement them to ensure thorough and efficient testing.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads