Data-driven tests let us check many input cases easily. This helps find more problems and makes tests stronger.
When you want to test a form with many different input values.
When you need to check how a website behaves with various user data.
When you want to run the same test steps but with different data sets.
When you want to avoid writing many similar tests manually.
When you want to improve test coverage without extra effort.
Syntax
Selenium Python
for data in test_data:
perform_test(data)
Use a loop to run the same test with different data.
Test data can come from lists, files, or databases.
Examples
This example runs a login test for three users.
Selenium Python
test_data = ["user1", "user2", "user3"]
for user in test_data:
print(f"Testing login for {user}")
This example tests login with username and password pairs.
Selenium Python
test_data = [("user1", "pass1"), ("user2", "pass2")]
for username, password in test_data:
print(f"Testing with {username} and {password}")
Sample Program
This test tries logging in with different usernames and passwords. It checks if login succeeds or fails as expected. This way, many cases are covered with one test function.
Selenium Python
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
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, "login-button").click()
time.sleep(1) # wait for page to load
success = "Welcome"in driver.page_source
driver.quit()
assert success, f"Login failed for {username}"# Data sets to test
credentials = [
("validUser", "validPass"),
("invalidUser", "validPass"),
("validUser", "wrongPass")
]
for user, pwd in credentials:
try:
test_login(user, pwd)
print(f"Test passed for {user}")
except AssertionError as e:
print(e)
OutputSuccess
Important Notes
Data-driven tests reduce repeated code and save time.
Make sure test data covers both normal and edge cases.
Keep test data organized and easy to update.
Summary
Data-driven tests run the same steps with many inputs.
This increases test coverage and finds more bugs.
It makes tests easier to maintain and expand.
Practice
(1/5)
1. Why do data-driven tests increase test coverage in Selenium Python?
easy
A. Because they skip validation steps
B. Because they reduce the number of test cases to one
C. Because they only test the UI layout
D. Because they run the same test steps with many different inputs
Solution
Step 1: Understand data-driven testing concept
Data-driven tests repeat the same test steps but use different input data sets.
Step 2: Connect data variation to coverage
Using many inputs helps test more scenarios, increasing coverage and finding more bugs.
Final Answer:
Because they run the same test steps with many different inputs -> Option D
Quick Check:
Data-driven tests = many inputs = more coverage [OK]
Hint: More inputs mean more scenarios tested [OK]
Common Mistakes:
Thinking data-driven tests reduce test cases
Confusing UI layout testing with data-driven testing
Believing data-driven tests skip validations
2. Which of the following is the correct way to pass multiple data sets to a Selenium Python test using pytest parameterization?
easy
A. @pytest.parametrization('input', [1, 2, 3])
B. @pytest.parametrize('input', (1, 2, 3))
C. @pytest.mark.parametrize('input', [1, 2, 3])
D. @pytest.mark.parametrized('input', [1, 2, 3])
Solution
Step 1: Recall pytest parameterization syntax
The correct decorator is @pytest.mark.parametrize with 'param' and list of values.
Step 2: Check each option syntax
Only @pytest.mark.parametrize('input', [1, 2, 3]) uses correct spelling and syntax: @pytest.mark.parametrize('input', [1, 2, 3]).
Final Answer:
@pytest.mark.parametrize('input', [1, 2, 3]) -> Option C