Parameterization

Introduction

In automation testing, it is common to execute the same test with different sets of input data. Writing separate test functions for each input makes the test suite longer, harder to maintain, and introduces unnecessary code duplication.

PyTest provides Parameterization, which allows a single test function to run multiple times using different input values. This helps create cleaner, more maintainable, and data-driven Selenium tests.

In this tutorial, you’ll learn what Parameterization is, how it works, and how to use @pytest.mark.parametrize to execute the same Selenium test with multiple test data values.


What is Parameterization?

Parameterization is a PyTest feature that executes the same test multiple times using different input values.

Instead of writing multiple test functions, you write one test and supply different data sets.

Example:

           One Test Function

                  │

                  ▼

     ┌────────────┼────────────┐
     │            │            │

"Selenium"    "PyTest"    "Automation"

     │            │            │

     ▼            ▼            ▼

 Test Run 1   Test Run 2   Test Run 3

Each input value produces a separate test execution.


Why Use Parameterization?

Parameterization helps you:

  • Eliminate duplicate test code.

  • Execute the same test with multiple inputs.

  • Improve test readability.

  • Support data-driven testing.

  • Simplify test maintenance.


How Parameterization Works

PyTest uses the @pytest.mark.parametrize decorator.

Example:

@pytest.mark.parametrize(
    "message",
    [
        "Hello",
        "Python",
        "Selenium",
    ],
)

Here:

  • message is the test parameter.

  • The list contains the values supplied to the test.

  • PyTest executes the test once for each value.


Example

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By


# Topic: 41. Advanced PyTest - Parameterization
# Practice site: https://www.testmuai.com/selenium-playground/simple-form-demo
# Run: pytest -s 41_examples/test_01_parameterization.py
#
# @pytest.mark.parametrize runs the same test with multiple input values.


@pytest.mark.parametrize(
    "message",
    [
        "Selenium",
        "PyTest",
        "Automation",
    ],
)
def test_parameterization(message):
    driver = webdriver.Chrome()

    try:
        driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
        driver.find_element(By.ID, "user-message").send_keys(message)
        driver.find_element(By.ID, "showInput").click()

        assert driver.find_element(By.ID, "message").text == message
    finally:
        driver.quit()

Understanding the Code

Import Required Libraries

import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By

These modules are required to:

  • Create parameterized tests.

  • Launch the Chrome browser.

  • Locate web elements.


Create a Parameterized Test

@pytest.mark.parametrize(
    "message",
    [
        "Selenium",
        "PyTest",
        "Automation",
    ],
)

The @pytest.mark.parametrize decorator tells PyTest to execute the same test multiple times.

Each value in the list is assigned to the message parameter during a separate test execution.


Define the Test Function

def test_parameterization(message):

The message parameter receives one value from the parameter list during each test run.

PyTest automatically supplies the values.


Launch the Browser

driver = webdriver.Chrome()

Starts a new Chrome browser session.

Each parameterized test execution launches its own browser.


Open the Practice Website

driver.get(
    "https://www.testmuai.com/selenium-playground/simple-form-demo"
)

Navigates to the Simple Form Demo page.


Enter the Message

driver.find_element(
    By.ID,
    "user-message"
).send_keys(message)

Locates the message input field and enters the current parameter value.

For example:

  • First execution → Selenium

  • Second execution → PyTest

  • Third execution → Automation


Click the Show Message Button

driver.find_element(
    By.ID,
    "showInput"
).click()

Clicks the Show Message button.

The application displays the entered message.


Verify the Result

assert driver.find_element(
    By.ID,
    "message"
).text == message

The assert statement verifies that the displayed message matches the current parameter value.

If the displayed message differs from the expected value, PyTest marks that test execution as failed.


Close the Browser

driver.quit()

Closes all browser windows and ends the WebDriver session.

The browser is closed after each parameterized test execution.


Practical Example

Suppose you want to verify that a search textbox accepts different search terms.

Instead of creating three separate tests, you can use Parameterization to execute one test with multiple search keywords.


Automation Testing Example

Consider an e-commerce website.

The automation script verifies product searches using different keywords:

  • Laptop

  • Mobile

  • Headphones

  • Keyboard

A single parameterized test executes once for each keyword, reducing duplicate code.


Real-World Example

Parameterization is commonly used in:

  • Login testing

  • Search functionality

  • Form validation

  • Data-driven testing

  • Regression testing

  • API testing

  • Enterprise automation frameworks

It is one of the most frequently used features in professional Selenium + PyTest projects.


Advantages of Parameterization

  • Eliminates duplicate test methods.

  • Improves code readability.

  • Supports multiple input values.

  • Simplifies maintenance.

  • Encourages data-driven testing.

  • Reduces overall test code.


Common Mistakes Beginners Make

Writing Separate Tests for Every Input

Instead of creating multiple nearly identical test functions, use @pytest.mark.parametrize to execute one test with different values.


Using the Wrong Parameter Name

Ensure the parameter name in @pytest.mark.parametrize matches the parameter in the test function.

For example:

@pytest.mark.parametrize("message", [...])

def test_example(message):

Both names must match.


Providing Incorrect Data

Each value supplied to the parameter list should match the expected input type used by the test.


Forgetting That Each Value Creates a New Test

PyTest treats every parameter value as a separate test execution.

If one parameter fails, the remaining parameterized tests continue executing independently.


Best Practices

  • Use @pytest.mark.parametrize to eliminate duplicate tests.

  • Keep parameter lists meaningful and easy to understand.

  • Use descriptive parameter names.

  • Keep each parameterized test focused on one scenario.

  • Combine Parameterization with Selenium to build data-driven automation tests.


Conclusion

Parameterization is one of the most powerful features of PyTest. It allows the same Selenium test to execute multiple times with different input values, reducing duplicate code while improving readability and maintainability. Learning how to use @pytest.mark.parametrize is an essential skill for creating efficient, scalable, and professional automation frameworks.


Frequently Asked Questions (FAQs)

What is Parameterization in PyTest?

Parameterization allows a single test function to execute multiple times using different input values.


Which decorator is used for Parameterization?

Use:

@pytest.mark.parametrize()

to create parameterized tests.


How many times does a parameterized test execute?

The test executes once for every value supplied in the parameter list.

For example, three values result in three separate test executions.


Can Parameterization be used with Selenium?

Yes.

Parameterization is commonly used with Selenium to test multiple input values such as usernames, passwords, search terms, and form data.


Why is Parameterization important?

It reduces duplicate code, improves maintainability, supports data-driven testing, and simplifies Selenium automation projects.


Key Takeaways

  • Parameterization executes the same test with multiple input values.

  • Use @pytest.mark.parametrize to create parameterized tests.

  • Each parameter value produces a separate test execution.

  • Parameterization reduces duplicate test code.

  • It is widely used in Selenium for data-driven testing.

  • Parameterization is an essential PyTest feature for building scalable automation frameworks.