Bird
Raised Fist0
Selenium Pythontesting~20 mins

Data providers pattern in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Provider Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of test execution with data provider
Given the following Selenium test code using a data provider pattern, what will be the output when running the test?
Selenium Python
import unittest
from parameterized import parameterized

class TestLogin(unittest.TestCase):

    @parameterized.expand([
        ("user1", "pass1"),
        ("user2", "pass2"),
        ("user3", "pass3")
    ])
    def test_login(self, username, password):
        print(f"Testing login with {username} and {password}")
        self.assertTrue(len(username) > 0)
        self.assertTrue(len(password) > 0)

if __name__ == '__main__':
    unittest.main()
AThree tests run, but all fail due to assertion error
BOne test runs, printing 'Testing login with user1 and pass1', passes
CThree tests run, each printing 'Testing login with userX and passX', all pass
DTest fails to run due to missing data provider decorator
Attempts:
2 left
💡 Hint
Look at how many parameter sets are provided to the @parameterized.expand decorator.
❓ assertion
intermediate
1:30remaining
Correct assertion for data-driven test
In a data-driven Selenium test, which assertion correctly verifies that the page title contains the expected keyword for each data set?
Selenium Python
def test_page_title(self, keyword):
    title = self.driver.title
    # Which assertion is correct here?
Aself.assertFalse(keyword in title)
Bself.assertIn(keyword, title)
Cself.assertEqual(keyword, title)
Dself.assertTrue(title.startswith(keyword))
Attempts:
2 left
💡 Hint
We want to check if the keyword appears anywhere in the title.
❓ locator
advanced
1:30remaining
Best locator strategy for data-driven tests
In a data-driven Selenium test, you need to locate a button whose text changes based on test data. Which locator strategy is best to use?
ALocate by XPath using contains(text(), variable_text)
BLocate by fixed ID attribute
CLocate by CSS class name
DLocate by tag name only
Attempts:
2 left
💡 Hint
The button text changes per test data, so the locator must adapt dynamically.
🔧 Debug
advanced
2:00remaining
Debugging failing data-driven test
A data-driven Selenium test fails intermittently with a NoSuchElementException on a locator that worked before. What is the most likely cause?
AThe page content changes based on test data, so the element is missing for some inputs
BThe locator syntax is invalid and causes a runtime error
CThe test data provider is empty, so no tests run
DThe browser driver is not installed
Attempts:
2 left
💡 Hint
Think about how test data can affect page elements.
❓ framework
expert
2:00remaining
Implementing data providers in pytest for Selenium
Which pytest feature allows you to implement data providers for Selenium tests to run the same test with multiple data sets?
Aunittest.TestCase subclass with setUp method
BUsing Selenium WebDriverWait explicitly
Cpytest.fixture with scope='module'
D@pytest.mark.parametrize decorator
Attempts:
2 left
💡 Hint
Look for a pytest decorator that runs tests multiple times with different inputs.

Practice

(1/5)
1. What is the main purpose of using the data providers pattern in Selenium Python tests?
easy
A. To generate test reports in different formats
B. To speed up the browser loading time
C. To create new test cases automatically
D. To run the same test multiple times with different input data

Solution

  1. Step 1: Understand the data providers pattern

    The data providers pattern allows running one test repeatedly with different sets of data.
  2. Step 2: Identify the main benefit

    This helps find more bugs by testing various inputs without writing separate tests.
  3. Final Answer:

    To run the same test multiple times with different input data -> Option D
  4. Quick Check:

    Data providers = run test many times with different data [OK]
Hint: Remember: data providers = repeat test with new data [OK]
Common Mistakes:
  • Thinking data providers speed up browser loading
  • Confusing data providers with automatic test creation
  • Believing data providers generate reports
2. Which of the following is the correct syntax to use a data provider in pytest for Selenium Python?
easy
A. @pytest.mark.parametrize('input', ['data1', 'data2'])
B. @pytest.run_with_data(['data1', 'data2'])
C. @pytest.test_data(['data1', 'data2'])
D. @pytest.data_provider(['data1', 'data2'])

Solution

  1. Step 1: Recall pytest data provider syntax

    Pytest uses @pytest.mark.parametrize to supply multiple data sets to a test function.
  2. Step 2: Match the correct syntax

    @pytest.mark.parametrize('input', ['data1', 'data2']) correctly shows @pytest.mark.parametrize('input', ['data1', 'data2']).
  3. Final Answer:

    @pytest.mark.parametrize('input', ['data1', 'data2']) -> Option A
  4. Quick Check:

    Pytest data provider = @pytest.mark.parametrize [OK]
Hint: Look for @pytest.mark.parametrize for data providers [OK]
Common Mistakes:
  • Using non-existent decorators like @pytest.data_provider
  • Confusing parametrize with test_data or run_with_data
  • Missing quotes around parameter names
3. What will be the output of the following pytest test using data providers?
import pytest

@pytest.mark.parametrize('num', [1, 2, 3])
def test_is_positive(num):
    assert num > 0
medium
A. All tests pass
B. All tests fail
C. Only the test with num=1 passes
D. Syntax error occurs

Solution

  1. Step 1: Analyze the test logic

    The test asserts that the input number is greater than zero.
  2. Step 2: Check each data value

    Values 1, 2, and 3 are all greater than zero, so all assertions pass.
  3. Final Answer:

    All tests pass -> Option A
  4. Quick Check:

    All nums > 0, so tests pass [OK]
Hint: Check if all inputs satisfy the assertion [OK]
Common Mistakes:
  • Assuming some inputs fail without checking values
  • Thinking syntax error due to parametrize
  • Ignoring that all numbers are positive
4. Identify the error in this pytest data provider usage:
@pytest.mark.parametrize('input', [1, 2, 3])
def test_example():
    assert input > 0
medium
A. Wrong decorator name
B. Missing parameter in test function definition
C. List of inputs should be strings
D. Assertion syntax is incorrect

Solution

  1. Step 1: Check test function parameters

    The test function must accept the parameter named in parametrize, here 'input'.
  2. Step 2: Identify missing parameter

    The function test_example() has no parameters, so 'input' is undefined inside it.
  3. Final Answer:

    Missing parameter in test function definition -> Option B
  4. Quick Check:

    Parametrize param must match function argument [OK]
Hint: Function must accept all parametrize arguments [OK]
Common Mistakes:
  • Forgetting to add parameter to test function
  • Using wrong decorator name
  • Thinking input list must be strings
  • Misreading assertion syntax
5. You want to test a login form with multiple username and password pairs using data providers in pytest. Which approach correctly handles multiple parameters?
hard
A. @pytest.mark.parametrize('username', ['user1', 'user2']) @pytest.mark.parametrize('password', ['pass1', 'pass2']) def test_login(username, password): assert username and password
B. @pytest.mark.parametrize('username_password', [('user1', 'pass1'), ('user2', 'pass2')]) def test_login(username_password): assert username_password[0] and username_password[1]
C. @pytest.mark.parametrize(('username', 'password'), [('user1', 'pass1'), ('user2', 'pass2')]) def test_login(username, password): assert username and password
D. @pytest.mark.parametrize('username', ['user1', 'user2'], 'password', ['pass1', 'pass2']) def test_login(username, password): assert username and password

Solution

  1. Step 1: Understand multiple parameter usage in parametrize

    To pass multiple parameters together, use a tuple of parameter names and a list of tuples with values.
  2. Step 2: Identify correct syntax

    @pytest.mark.parametrize(('username', 'password'), [('user1', 'pass1'), ('user2', 'pass2')]) def test_login(username, password): assert username and password uses @pytest.mark.parametrize(('username', 'password'), [('user1', 'pass1'), ('user2', 'pass2')]), which is correct.
  3. Final Answer:

    Use tuple of parameter names and list of tuples for values -> Option C
  4. Quick Check:

    Multiple params = tuple names + list of tuples [OK]
Hint: Use tuple of names and list of tuples for multiple params [OK]
Common Mistakes:
  • Stacking multiple parametrize decorators incorrectly
  • Passing multiple parameters in one string without tuple
  • Wrong syntax with multiple lists in one decorator
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard