What does it mean to parameterize tests with test data?
Parameterizing tests means running the same test multiple times with different input values. This helps check how the software behaves with various data without writing separate tests for each case.
Click to reveal answer
beginner
Why is parameterizing tests useful in Selenium testing?
It saves time by reusing the same test code for many inputs. It also helps find bugs that only appear with certain data, making tests more thorough and efficient.
Click to reveal answer
intermediate
How can you parameterize tests in Python using pytest?
You can use the @pytest.mark.parametrize decorator to pass a list of input values to a test function. The test runs once for each set of inputs automatically.
Click to reveal answer
intermediate
Show a simple example of parameterizing a Selenium test with pytest.
Example:
import pytest
from selenium.webdriver.common.by import By
@pytest.mark.parametrize('username,password', [
('user1', 'pass1'),
('user2', 'pass2')
])
def test_login(driver, username, password):
driver.get('http://example.com/login')
driver.find_element(By.ID, 'user').send_keys(username)
driver.find_element(By.ID, 'pass').send_keys(password)
driver.find_element(By.ID, 'submit').click()
assert 'Welcome' in driver.page_source
Click to reveal answer
intermediate
What are best practices when choosing test data for parameterized tests?
Choose data that covers normal cases, edge cases, and invalid inputs. This ensures the software is tested well and can handle different situations gracefully.
Click to reveal answer
What is the main benefit of parameterizing tests?
ARun the same test with different inputs automatically
BWrite separate tests for each input
CSkip tests that fail
DMake tests run faster by skipping assertions
✗ Incorrect
Parameterizing tests allows running the same test multiple times with different inputs without rewriting the test.
Which Python decorator is commonly used to parameterize tests in pytest?
A@pytest.mark.parametrize
B@pytest.testdata
C@pytest.run
D@pytest.input
✗ Incorrect
The @pytest.mark.parametrize decorator is used to pass multiple sets of data to a test function.
In Selenium tests, what should you do after entering parameterized input data?
AIgnore the input and check logs
BClose the browser immediately
CClick submit or perform the action to test
DRestart the test
✗ Incorrect
After entering input data, you perform the action (like clicking submit) to test the behavior with that data.
Which of these is NOT a good type of test data to include when parameterizing?
ANormal expected inputs
BEdge cases like empty strings
CInvalid or unexpected inputs
DOnly one fixed input
✗ Incorrect
Using only one fixed input defeats the purpose of parameterization, which is to test multiple data cases.
What happens if an assertion fails in one parameterized test case?
AAll test cases stop immediately
BOnly that test case fails, others continue
CThe test runner ignores the failure
DThe test automatically retries
✗ Incorrect
In parameterized tests, each input set runs as a separate test case. Failure in one does not stop others.
Explain how to use pytest to parameterize a Selenium test with multiple username and password pairs.
Think about how to repeat the same test with different inputs automatically.
You got /4 concepts.
Describe why parameterizing tests improves test coverage and efficiency.
Consider how testing many cases helps find more issues without extra work.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of using @pytest.mark.parametrize in Selenium Python tests?
easy
A. To run tests in parallel threads
B. To skip tests that are not ready
C. To run the same test function multiple times with different input data
D. To generate HTML reports automatically
Solution
Step 1: Understand the decorator's role
@pytest.mark.parametrize allows 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 C
Quick Check:
Parameterize = run test with many inputs [OK]
Hint: Parameterize means run test repeatedly with different data [OK]
Common Mistakes:
Confusing parameterize with skipping tests
Thinking it runs tests in parallel
Assuming it generates reports
2. Which of the following is the correct syntax to parameterize a test function with two sets of inputs ("user1", "pass1") and ("user2", "pass2") in pytest?
easy
A. @pytest.parametrize('username', ['user1', 'user2'], 'password', ['pass1', 'pass2'])
B. @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')])
C. @pytest.mark.parametrize(['username', 'password'], 'user1', 'pass1', 'user2', 'pass2')
D. @pytest.mark.parametrized('username', 'password', [('user1', 'pass1'), ('user2', 'pass2')])
Solution
Step 1: Recall correct decorator syntax
The correct syntax uses @pytest.mark.parametrize with 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 B
A. Missing list of parameter values in @pytest.mark.parametrize decorator
B. Function parameters do not match decorator parameters
C. Assertion logic is incorrect
D. pytest module is not imported
Solution
Step 1: Check decorator usage
The decorator @pytest.mark.parametrize requires 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 A
Quick Check:
Parametrize needs names and values [OK]
Hint: Always provide parameter values list with parametrize [OK]
Common Mistakes:
Forgetting the list of test data tuples
Mismatching parameter names and function arguments
Incorrect assertion logic
5. You want to test a login form with multiple username and password pairs using Selenium and pytest. Which approach correctly applies parameterization and Selenium WebDriver usage together?
hard
A. Use @pytest.mark.parametrize to pass username and password, then inside the test open the browser, enter credentials, and assert login success
B. Write separate test functions for each username and password pair without parameterization
C. Use a loop inside a single test function to iterate over username and password pairs without parameterization
D. Use @pytest.mark.parametrize but open the browser only once outside the test function
Solution
Step 1: Understand parameterization with Selenium
Parameterization with @pytest.mark.parametrize allows 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 A
Quick Check:
Parametrize + fresh browser per test = best practice [OK]
Hint: Parametrize inputs and open browser inside test for isolation [OK]
Common Mistakes:
Opening browser once for all tests causing state issues
Not using parameterize and duplicating test code
Using loops inside tests instead of parameterize
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.