Parameterized tests help you run the same test with different data easily. This saves time and avoids repeating code.
When you want to check a login form with multiple usernames and passwords.
When you need to test a search feature with different keywords.
When you want to verify a calculator app with many input values.
When you want to test form validation with various input combinations.
Syntax
Selenium Python
from pytest import mark
@mark.parametrize("input,expected", [
(value1, expected1),
(value2, expected2),
# ... add more test cases
])
def test_function(input, expected):
# test steps using inputassert actual_result == expected
Use @mark.parametrize decorator from pytest to pass multiple data sets.
Each tuple in the list represents one test case with inputs and expected results.
Examples
This test checks if the square of a number matches the expected value.
Selenium Python
from pytest import mark
@mark.parametrize("number,expected", [
(2, 4),
(3, 9),
(4, 16)
])
def test_square(number, expected):
assert number * number == expected
This test tries to log in with different usernames and passwords using Selenium WebDriver.
Selenium Python
from pytest import mark
from selenium.webdriver.common.by import By
@mark.parametrize("username,password", [
("user1", "pass1"),
("user2", "pass2")
])
def test_login(username, password, driver):
driver.get("https://example.com/login")
driver.find_element(By.ID, "username").send_keys(username)
driver.find_element(By.ID, "password").send_keys(password)
driver.find_element(By.ID, "submit").click()
assert"Welcome"in driver.page_source
Sample Program
This test opens a website and searches for different fruits. It checks if the search term appears in the page title after searching.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from pytest import mark
@mark.parametrize("search_term", ["apple", "banana", "cherry"])
def test_search_function(search_term):
driver = webdriver.Chrome()
driver.get("https://www.example.com")
search_box = driver.find_element(By.NAME, "q")
search_box.clear()
search_box.send_keys(search_term)
search_box.submit()
assert search_term in driver.title.lower()
driver.quit()
OutputSuccess
Important Notes
Always close the browser after each test to avoid resource leaks.
Use clear and meaningful parameter names for readability.
Parameterized tests help find bugs with different inputs quickly.
Summary
Parameterized tests run the same test with many data sets.
They reduce repeated code and save time.
Use pytest's @mark.parametrize decorator for easy implementation.
Practice
(1/5)
1. What is the main benefit of using @pytest.mark.parametrize in Selenium Python tests?
easy
A. It allows running the same test with multiple sets of data easily.
B. It speeds up the browser loading time.
C. It automatically finds locators on the page.
D. It generates test reports without extra code.
Solution
Step 1: Understand the purpose of parameterized tests
Parameterized tests run the same test logic multiple times with different input data sets.
Step 2: Identify what @pytest.mark.parametrize does
This decorator allows easy setup of multiple data inputs for one test function, reducing repeated code.
Final Answer:
It allows running the same test with multiple sets of data easily. -> Option A
Quick Check:
Parameterized tests = multiple data sets [OK]
Hint: Think: same test, many inputs, less code [OK]
Common Mistakes:
Confusing parameterization with browser speed optimization
Thinking it auto-finds locators
Assuming it auto-generates reports
2. Which of the following is the correct syntax to parameterize a test function with two parameters username and password using pytest?
easy
A. @pytest.mark.param('username', 'password', [('user1', 'pass1'), ('user2', 'pass2')])
B. @pytest.parametrize(['username', 'password'], [('user1', 'pass1'), ('user2', 'pass2')])
C. @pytest.mark.parametrize(('username', 'password'), ['user1', 'pass1', 'user2', 'pass2'])
D. @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')])
Solution
Step 1: Recall pytest parameterize syntax
The correct syntax uses @pytest.mark.parametrize with a string of comma-separated parameter names and a list of tuples for values.
Step 2: Compare options
@pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) matches the correct syntax exactly. Others have wrong decorator names or wrong argument formats.
Final Answer:
@pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) -> Option D
Quick Check:
Correct decorator and tuple list = @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) [OK]
Hint: Remember: mark.parametrize with string params and list of tuples [OK]
Common Mistakes:
Misspelling 'parametrize' as 'parametrize' or 'param'
Using list instead of tuple for parameter values
Passing parameters as list instead of string
3. What will be the output when running this parameterized test snippet?
B. Missing data list for parameterize decorator on test_double.
C. Parameters names should be a list, not a string.
D. Test function name is invalid.
Solution
Step 1: Check the decorator usage on test_double
The decorator @pytest.mark.parametrize('input, expected') is missing the second argument: the list of data tuples.
Step 2: Verify other parts
Assertion and function name are correct. Parameter names as string is valid syntax.
Final Answer:
Missing data list for parameterize decorator on test_double. -> Option B
Quick Check:
Decorator needs data list argument [OK]
Hint: Always provide data list with parametrize decorator [OK]
Common Mistakes:
Forgetting the data list argument
Using list instead of string for parameter names (which is allowed but less common)
Misreading assertion syntax
5. You want to test a login form with multiple username and password pairs using Selenium and pytest parameterization. Which approach correctly combines parameterized tests with Selenium WebDriver to test login functionality?
hard
A. Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, fill fields, submit, and assert login success.
B. Write separate test functions for each username and password pair without parameterization.
C. Use parameterization only for URLs, not for form inputs like username and password.
D. Use @pytest.mark.parametrize but do not open the browser inside the test function.
Solution
Step 1: Understand parameterized test purpose in Selenium context
Parameterized tests allow running the same test logic with different input data, such as username and password pairs.
Step 2: Combine parameterization with Selenium actions
Inside the test function, open the browser, fill the login form with parameters, submit, and check results. This is the correct approach.
Final Answer:
Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, fill fields, submit, and assert login success. -> Option A
Quick Check:
Parameterize inputs + Selenium actions inside test [OK]
Hint: Parameterize inputs, then automate browser steps inside test [OK]
Common Mistakes:
Writing separate tests instead of parameterizing
Not opening browser inside test function
Limiting parameterization to URLs only
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.