Parameterizing tests means running the same test with different data. This helps check many cases without writing many tests.
Parameterize with test data in Selenium Python
from pytest import mark @mark.parametrize("input,expected", [ ("data1", "result1"), ("data2", "result2"), ]) def test_example(input, expected): assert function_to_test(input) == expected
Use @pytest.mark.parametrize decorator to pass multiple sets of data.
Each tuple in the list represents one test run with its inputs and expected output.
from pytest import mark @mark.parametrize("username,password", [ ("user1", "pass1"), ("user2", "pass2"), ]) def test_login(username, password): assert login(username, password) == True
from pytest import mark @mark.parametrize("search_term", ["apple", "banana", "cherry"]) def test_search(search_term): assert search(search_term) != []
This test opens a login page, enters username and password from the parameterized data, clicks submit, and checks the success message.
It runs twice with two different username-password pairs.
from selenium import webdriver from selenium.webdriver.common.by import By import pytest @pytest.mark.parametrize("username,password", [ ("user1", "pass1"), ("user2", "pass2"), ]) def test_login(username, password): driver = webdriver.Chrome() 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() message = driver.find_element(By.ID, "message").text driver.quit() assert message == "Login successful" # Run this with pytest to see two tests run with different data.
Always use clear and meaningful test data for better understanding.
Parameterizing helps avoid repeating similar test code.
Make sure your test cleans up after each run to avoid side effects.
Parameterize tests to run the same test with different data easily.
Use @pytest.mark.parametrize decorator in Python Selenium tests.
This saves time and finds more bugs by testing many cases.
Practice
@pytest.mark.parametrize in Selenium Python tests?Solution
Step 1: Understand the decorator's role
@pytest.mark.parametrizeallows running one test function multiple times with different sets of input data.Step 2: Compare with other options
Skipping tests, running in parallel, or generating reports are different pytest features, not related to parameterization.Final Answer:
To run the same test function multiple times with different input data -> Option CQuick Check:
Parameterize = run test with many inputs [OK]
- Confusing parameterize with skipping tests
- Thinking it runs tests in parallel
- Assuming it generates reports
("user1", "pass1") and ("user2", "pass2") in pytest?Solution
Step 1: Recall correct decorator syntax
The correct syntax uses@pytest.mark.parametrizewith a string of parameter names and a list of tuples for values.Step 2: Identify correct option
@pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) matches the correct syntax exactly. Other options have wrong method names or argument formats.Final Answer:
@pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) -> Option BQuick Check:
Parametrize('params', [tuples]) = correct syntax [OK]
- Misspelling 'parametrize' as 'parametrized' or 'parametrize'
- Passing parameters as separate lists instead of tuples
- Using wrong decorator name
import pytest
@pytest.mark.parametrize('num, expected', [(1, 2), (3, 4)])
def test_increment(num, expected):
assert num + 1 == expectedSolution
Step 1: Analyze each test case
For (1, 2): 1 + 1 = 2, assertion passes. For (3, 4): 3 + 1 = 4, assertion passes.Step 2: Determine overall result
Since both assertions pass, both tests pass successfully.Final Answer:
Both tests pass -> Option DQuick Check:
num+1 == expected for all inputs [OK]
- Assuming one test fails without checking values
- Confusing expected values with actual calculation
- Ignoring that both inputs are tested
import pytest
@pytest.mark.parametrize('username, password')
def test_login(username, password):
assert username != '' and password != ''Solution
Step 1: Check decorator usage
The decorator@pytest.mark.parametrizerequires two arguments: parameter names and a list of values. Here, the list of values is missing.Step 2: Verify other parts
Function parameters match decorator names, assertion logic is valid, and pytest is imported correctly.Final Answer:
Missing list of parameter values in @pytest.mark.parametrize decorator -> Option AQuick Check:
Parametrize needs names and values [OK]
- Forgetting the list of test data tuples
- Mismatching parameter names and function arguments
- Incorrect assertion logic
Solution
Step 1: Understand parameterization with Selenium
Parameterization with@pytest.mark.parametrizeallows running the test multiple times with different data, opening the browser fresh each time ensures test isolation.Step 2: Evaluate other options
Writing separate tests duplicates code, looping inside one test loses pytest reporting benefits, and opening browser only once risks test interference.Final Answer:
Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, enter credentials, and assert login success -> Option AQuick Check:
Parametrize + fresh browser per test = best practice [OK]
- Opening browser once for all tests causing state issues
- Not using parameterize and duplicating test code
- Using loops inside tests instead of parameterize
Ready to prove it?
Pass the test and this pattern moves unknown
