What if you could test dozens of input cases with just one simple test?
Why Parameterized tests in Selenium Python? - Purpose & Use Cases
Imagine you have a web form that needs to be tested with many different input values. You open the browser, type each value one by one, submit the form, and check the result manually every time.
This manual way is slow and boring. You might forget some test cases or make mistakes typing inputs. It's hard to keep track of what you tested and what you missed. If the form changes, you must redo everything.
Parameterized tests let you write one test that runs many times with different inputs automatically. You just list the inputs once, and the test framework repeats the test for each input. This saves time and avoids errors.
def test_login(): login('user1', 'pass1') assert check_success() login('user2', 'pass2') assert check_success()
@pytest.mark.parametrize('username,password', [('user1','pass1'), ('user2','pass2')]) def test_login(username, password): login(username, password) assert check_success()
It enables fast, reliable testing of many input cases with less code and less chance of mistakes.
Testing a signup form with dozens of valid and invalid email and password combinations becomes easy and automatic with parameterized tests.
Manual repetitive tests are slow and error-prone.
Parameterized tests run one test many times with different inputs automatically.
This approach saves time and improves test coverage.
Practice
@pytest.mark.parametrize in Selenium Python tests?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
This decorator allows easy setup of multiple data inputs for one test function, reducing repeated code.@pytest.mark.parametrizedoesFinal Answer:
It allows running the same test with multiple sets of data easily. -> Option AQuick Check:
Parameterized tests = multiple data sets [OK]
- Confusing parameterization with browser speed optimization
- Thinking it auto-finds locators
- Assuming it auto-generates reports
username and password using pytest?Solution
Step 1: Recall pytest parameterize syntax
The correct syntax uses@pytest.mark.parametrizewith 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 DQuick Check:
Correct decorator and tuple list = @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) [OK]
- Misspelling 'parametrize' as 'parametrize' or 'param'
- Using list instead of tuple for parameter values
- Passing parameters as list instead of string
import pytest
@pytest.mark.parametrize('num, expected', [(1, 2), (2, 3), (3, 4)])
def test_increment(num, expected):
assert num + 1 == expectedSolution
Step 1: Analyze the test logic and parameters
Each tuple providesnumandexpected. The test assertsnum + 1 == expected. For (1,2), 1+1=2; for (2,3), 2+1=3; for (3,4), 3+1=4.Step 2: Check if assertions hold for all cases
All assertions are true, so all tests pass.Final Answer:
All tests pass successfully. -> Option CQuick Check:
num + 1 equals expected for all [OK]
- Assuming some tests fail without checking values
- Confusing parameter order
- Thinking decorator syntax is wrong
import pytest
@pytest.mark.parametrize('input, expected')
def test_double(input, expected):
assert input * 2 == expected
@pytest.mark.parametrize('input, expected', [(2, 4), (3, 6)])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 BQuick Check:
Decorator needs data list argument [OK]
- Forgetting the data list argument
- Using list instead of string for parameter names (which is allowed but less common)
- Misreading assertion syntax
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 AQuick Check:
Parameterize inputs + Selenium actions inside test [OK]
- Writing separate tests instead of parameterizing
- Not opening browser inside test function
- Limiting parameterization to URLs only
Ready to prove it?
Pass the test and this pattern moves unknown
