Introduction
Automation testing often requires executing the same test multiple times with different sets of input data. Writing separate test methods for each input combination leads to duplicate code, increases maintenance effort, and makes the test suite difficult to manage.
Parameterized Test Execution solves this problem by allowing a single test method to run multiple times using different input values. The test logic remains the same, while only the test data changes for each execution.
In PyTest, parameterization is achieved using the @pytest.mark.parametrize decorator. It automatically executes the same test once for every set of input data provided.
Parameterized execution is one of the core concepts of Data-Driven Testing and is widely used in Selenium automation frameworks to validate multiple scenarios with minimal code.
In this tutorial, you’ll learn what parameterized test execution is, why it is used, how PyTest parameterization works, and how to execute the same Selenium test with multiple data sets.
What is Parameterized Test Execution?
Parameterized Test Execution is a testing technique in which the same test method is executed multiple times using different input values.
Instead of creating multiple test methods, a single test receives different parameters during execution.
For example:
| Input 1 | Input 2 | Expected Output |
|---|---|---|
| 2 | 3 | 5 |
| 10 | 5 | 15 |
The automation framework executes the same test twice—once for each row of data.
Why Use Parameterized Test Execution?
Parameterized execution provides several advantages:
Eliminates duplicate test methods.
Reduces code duplication.
Improves test maintainability.
Supports Data-Driven Testing.
Executes multiple scenarios automatically.
Makes test cases easier to read.
Simplifies adding new test data.
Improves automation framework scalability.
Syntax of pytest.mark.parametrize
The basic syntax is:
@pytest.mark.parametrize(
"parameter1, parameter2",
[
(value1, value2),
(value3, value4)
]
)
PyTest automatically executes the test once for every tuple in the list.
Example
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
@pytest.mark.parametrize(
"a,b,expected",
[
("2", "3", "5"),
("10", "5", "15")
]
)
def test_parameterized_test_execution(a, b, expected):
driver = webdriver.Chrome()
try:
driver.get("https://www.testmuai.com/selenium-playground/simple-form-demo")
driver.find_element(By.ID, "sum1").send_keys(a)
driver.find_element(By.ID, "sum2").send_keys(b)
driver.find_element(
By.CSS_SELECTOR,
"#gettotal > button"
).click()
assert driver.find_element(
By.ID,
"addmessage"
).text == expected
finally:
driver.quit()
Understanding the Code
Import Required Modules
import pytest
from selenium import webdriver
from selenium.webdriver.common.by import By
The required modules are imported.
pytestprovides support for parameterized test execution.webdriverlaunches the browser.Byis used to locate web elements.
Create the Parameterized Test
@pytest.mark.parametrize(
"a,b,expected",
[
("2", "3", "5"),
("10", "5", "15")
]
)
The @pytest.mark.parametrize decorator supplies multiple sets of input data to the test method.
The parameters are:
a→ First numberb→ Second numberexpected→ Expected result
PyTest automatically executes the test once for each tuple.
Execution 1:
a = 2
b = 3
expected = 5
Execution 2:
a = 10
b = 5
expected = 15
Notice that the test method is written only once, but PyTest runs it twice.
Define the Test Method
def test_parameterized_test_execution(
a,
b,
expected
):
The test method receives the parameter values from the decorator.
There is no need to manually call the method for each dataset.
Launch the Browser
driver = webdriver.Chrome()
A new Chrome browser instance is created for each test execution.
Open the Application
driver.get(
"https://www.testmuai.com/selenium-playground/simple-form-demo"
)
The browser opens the Selenium Playground Simple Form Demo page.
Enter the First Number
driver.find_element(
By.ID,
"sum1"
).send_keys(a)
The first parameter (a) is entered into the first input field.
During the first execution, the value entered is:
2
During the second execution, the value entered is:
10
Enter the Second Number
driver.find_element(
By.ID,
"sum2"
).send_keys(b)
The second parameter (b) is entered into the second input field.
Click the Get Total Button
driver.find_element(
By.CSS_SELECTOR,
"#gettotal > button"
).click()
The Get Total button is clicked.
The application calculates the sum of the two numbers.
Verify the Result
assert driver.find_element(
By.ID,
"addmessage"
).text == expected
The displayed result is compared with the expected value supplied by the parameterized test data.
If both values match, the current execution passes successfully.
Close the Browser
finally:
driver.quit()
The browser is closed after each execution.
Since there are two parameter sets, the browser is launched and closed twice.
Practical Example
Suppose an e-commerce website provides a discount calculator.
Instead of writing separate Selenium tests for different product prices and discount percentages, parameterized execution runs the same test with multiple combinations of prices and discounts, verifying that the calculated discount is correct for each scenario.
Automation Testing Example
Consider an online banking application.
A money transfer feature needs to be tested with different transfer amounts and expected balances. Using PyTest parameterization, a single Selenium test executes repeatedly with multiple transaction values, verifying that the updated account balance is calculated correctly after each transfer.
Real-World Example
Parameterized test execution is widely used in:
Selenium Automation Frameworks
Login Testing
Form Validation
Calculator Applications
Banking Applications
E-commerce Websites
REST API Testing
Enterprise Automation Frameworks
Typical parameterized data includes usernames, passwords, search keywords, product quantities, transaction amounts, customer information, and expected results.
Advantages of Parameterized Test Execution
Eliminates duplicate test methods.
Executes multiple datasets automatically.
Improves code readability.
Simplifies maintenance.
Supports Data-Driven Testing.
Makes test cases reusable.
Easily accommodates additional test data.
Improves automation framework scalability.
Common Mistakes Beginners Make
Creating Separate Test Methods
Instead of writing multiple test methods with different inputs, use parameterization to execute one test with multiple datasets.
Providing Incorrect Parameter Counts
Ensure that each tuple contains the same number of values as the declared parameters.
Mixing Test Logic with Test Data
Keep parameter values inside the @pytest.mark.parametrize decorator while keeping the test logic separate.
Forgetting That Each Dataset Runs Independently
Each parameter set is treated as a separate test execution, with its own setup, execution, and teardown.
Best Practices
Use parameterization whenever the same test must run with different data.
Keep test logic independent of test data.
Use meaningful parameter names.
Store large datasets in external files such as Excel, CSV, JSON, or YAML when appropriate.
Keep parameter lists concise for readability.
Combine parameterization with Page Object Model (POM) for cleaner test design.
Use assertions to validate each execution independently.
Conclusion
Parameterized Test Execution is a powerful feature of PyTest that enables the same Selenium test to execute multiple times using different input values. It reduces duplicate code, improves maintainability, and supports Data-Driven Testing by separating test logic from test data. Because of its simplicity and flexibility, parameterized execution is widely used in professional Selenium automation frameworks.
Frequently Asked Questions (FAQs)
What is parameterized test execution?
Parameterized test execution is a technique where the same test method runs multiple times using different sets of input data.
Which PyTest feature is used for parameterization?
PyTest uses the @pytest.mark.parametrize decorator to execute tests with multiple datasets.
Why is parameterized testing used?
It eliminates duplicate test methods, improves maintainability, and supports Data-Driven Testing.
Can parameterized tests be used with Selenium?
Yes.
Parameterized execution is commonly used in Selenium automation to validate multiple input combinations using a single test script.
Is parameterized testing used in professional automation frameworks?
Yes.
Parameterized testing is a standard practice in Selenium automation frameworks for executing login tests, form validations, calculations, API testing, and many other scenarios using multiple datasets.
Key Takeaways
Parameterized Test Executionruns the same test multiple times using different input values.PyTest uses the
@pytest.mark.parametrizedecorator for parameterization.It reduces duplicate code and improves maintainability.
Parameterization is a core concept of Data-Driven Testing.
It is widely used in professional Selenium automation frameworks to execute multiple test scenarios efficiently.
