Understanding parameterise in Pytest

Understanding parameterise in Pytest

Parameterisation is a test data management technique, it is used for writing tests with effective test data management.

One of the most common use cases in test automation is executing a test for multiple sets of data.

In this case, if we write the same test for each data set, we are unnecessarily duplicating the code. This is what a good automation test engineer or programmer should not be doing.

The solution is, as a programmer, we should be running the same test for multiple sets of data instead of duplicating the code.

In Pytest, we have a marker "parameterize()" which takes multiple sets of data as input and runs the test for each data set.

@pytest.mark.parametrize("sequence of argument names", "list of sequence of argument values")

parameterize() marker takes two arguments mainly

  1. The sequence of arguments names - this sequence can be a list, set or tuple

  2. The list of the sequence of the argument values - this sequence also can be a list, set or tuple.

Let's understand it with an example, I have a test which takes Company Name, CEO and Key Product as inputs and prints the same.

name = "Google"
ceo = "Sundar"
product = "Chrome"

def test_print_company_details():
    print(f"\nLaunching the application {name}")
    print(f"Entering the username {ceo}")
    print(f"Entering the password {product}")

As shown in the above code snippet, the test is simply printing the values of the company.

Output:

platform linux -- Python 3.10.12, pytest-7.4.2, pluggy-1.3.0 -- /home/brahma/python_works/venv/bin/python
cachedir: .pytest_cache
rootdir: /home/brahma/python_works/pytest
plugins: dependency-0.5.1
collected 1 item                                                                                                                               

test_parameter_demo.py::test_print_company_details 
Launching the application Google
Entering the username Sundar
Entering the password Chrome
PASSED

Now let's parametrize it with Pytest marker "parameterize()".

import pytest

@pytest.mark.parametrize("name, ceo, product", [("Google", "Sundar", "Chrome"), ("Apple", "Tim", "iPhone")])
def test_print_company_details(name, ceo, product):
    print(f"\nLaunching the application {name}")
    print(f"Entering the username {ceo}")
    print(f"Entering the password {product}")

In the above test we can see that we are passing the arguments names "name", "ceo" and "product" as the first argument and argument values as list of tuples. The output will be as shown below.

test_parameter_demo.py::test_print_company_details[Google-Sundar-Chrome] 
Launching the application Google
Entering the username Sundar
Entering the password Chrome
PASSED
test_parameter_demo.py::test_print_company_details[Apple-Tim-iPhone] 
Launching the application Apple
Entering the username Tim
Entering the password iPhone
PASSED

The test is executed for two data sets as shown in the above output. In this way we can parametrize the tests.

We can pass the arguments in different ways, please checkout below:

Example 1: Arguments names as set, arguments values as list of tuple, set and list


@pytest.mark.parametrize({"name", "ceo", "product"}, [("Google", "Sundar", "Chrome"), ["Apple", "Tim", "iPhone"], {"Microsoft", "Satya", "Office 365"}])
def test_print_company_details_set(name, ceo, product):
    print(f"\nLaunching the application {name}")
    print(f"Entering the username {ceo}")
    print(f"Entering the password {product}")

Example 2: Arguments names as tuple, arguments values as tuple of tuple, set and list


@pytest.mark.parametrize(("name", "ceo", "product"), (("Google", "Sundar", "Chrome"), ["Apple", "Tim", "iPhone"], {"Microsoft", "Satya", "Office 365"}))
def test_print_company_details_tuple(name, ceo, product):
    print(f"\nLaunching the application {name}")
    print(f"Entering the username {ceo}")
    print(f"Entering the password {product}")

Example 3: Arguments names as list, arguments values as list of tuple, set and list

@pytest.mark.parametrize(["name", "ceo", "product"], (("Google", "Sundar", "Chrome"), ["Apple", "Tim", "iPhone"], {"Microsoft", "Satya", "Office 365"}))
def test_print_company_details_list(name, ceo, product):
    print(f"\nLaunching the application {name}")
    print(f"Entering the username {ceo}")
    print(f"Entering the password {product}")

Above are the different ways to pass the arguments and argument values to parametrize(). Thank you for reading. #python #pytest #programming #automation

Did you find this article valuable?

Support Brahma Rao Kothapalli by becoming a sponsor. Any amount is appreciated!