Bird
Raised Fist0
Selenium Pythontesting~20 mins

Parameterize with test data in Selenium Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parameterization Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parameterized test with multiple data sets
Given the following pytest test function using parameterization, what will be the output when running the test?
Selenium Python
import pytest

@pytest.mark.parametrize('input,expected', [(2, 4), (3, 9), (4, 16)])
def test_square(input, expected):
    assert input * input == expected

# What is the test result summary?
A3 tests passed
B2 tests passed, 1 failed
CAll tests failed
D1 test passed, 2 failed
Attempts:
2 left
💡 Hint
Check if the assertion matches the expected square values.
❓ assertion
intermediate
2:00remaining
Correct assertion for parameterized Selenium test
You have a Selenium test that checks page titles for multiple URLs using parameterization. Which assertion correctly verifies the page title matches the expected title?
Selenium Python
from selenium import webdriver
import pytest

@pytest.mark.parametrize('url,expected_title', [
    ('https://example.com', 'Example Domain'),
    ('https://www.python.org', 'Welcome to Python.org')
])
def test_page_title(url, expected_title):
    driver = webdriver.Chrome()
    driver.get(url)
    # Which assertion is correct here?
    # assert ???
    driver.quit()
Aassert expected_title in driver.page_source
Bassert driver.title != expected_title
Cassert driver.title == expected_title
Dassert driver.current_url == expected_title
Attempts:
2 left
💡 Hint
Page title is accessed by driver.title property.
🔧 Debug
advanced
2:00remaining
Identify the error in parameterized Selenium test code
What error will occur when running this parameterized Selenium test code?
Selenium Python
import pytest
from selenium import webdriver

@pytest.mark.parametrize('url', ['https://example.com', 'https://python.org'])
def test_open_page(url):
    driver = webdriver.Chrome
    driver.get(url)
    assert 'Example' in driver.title
    driver.quit()
AAttributeError: 'function' object has no attribute 'get'
BNo error, test passes
CNameError: name 'driver' is not defined
DTypeError: 'WebDriver' object is not callable
Attempts:
2 left
💡 Hint
Check how the webdriver.Chrome is used.
❓ framework
advanced
2:00remaining
Best practice for parameterizing Selenium tests with pytest fixtures
Which option shows the best way to combine pytest fixtures and parameterization for Selenium tests to reuse the browser instance efficiently?
AUse @pytest.mark.parametrize on test function and create a new driver inside each test
BUse @pytest.fixture with parameters and create driver inside fixture for each test
CCreate driver globally outside tests and parametrize tests without fixtures
DUse a session-scoped fixture to create driver once and parametrize test with @pytest.mark.parametrize
Attempts:
2 left
💡 Hint
Think about reusing browser instance to speed up tests.
🧠 Conceptual
expert
2:00remaining
Why parameterize tests in Selenium automation?
What is the main advantage of parameterizing Selenium tests with multiple data sets?
AIt makes tests run faster by parallelizing browser instances automatically
BIt reduces code duplication and allows running the same test logic with different inputs automatically
CIt ensures tests only run once with fixed data to avoid flaky results
DIt removes the need for assertions by validating all inputs implicitly
Attempts:
2 left
💡 Hint
Think about how parameterization affects test coverage and code reuse.

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

  1. Step 1: Understand the decorator's role

    @pytest.mark.parametrize allows running one test function multiple times with different sets of input data.
  2. Step 2: Compare with other options

    Skipping tests, running in parallel, or generating reports are different pytest features, not related to parameterization.
  3. Final Answer:

    To run the same test function multiple times with different input data -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    @pytest.mark.parametrize('username, password', [('user1', 'pass1'), ('user2', 'pass2')]) -> Option B
  4. Quick Check:

    Parametrize('params', [tuples]) = correct syntax [OK]
Hint: Use string param names and list of tuples for values [OK]
Common Mistakes:
  • Misspelling 'parametrize' as 'parametrized' or 'parametrize'
  • Passing parameters as separate lists instead of tuples
  • Using wrong decorator name
3. Given the following test code, what will be the output when running the test?
import pytest

@pytest.mark.parametrize('num, expected', [(1, 2), (3, 4)])
def test_increment(num, expected):
    assert num + 1 == expected
medium
A. First test fails, second test passes
B. Both tests fail
C. First test passes, second test fails
D. Both tests pass

Solution

  1. Step 1: Analyze each test case

    For (1, 2): 1 + 1 = 2, assertion passes. For (3, 4): 3 + 1 = 4, assertion passes.
  2. Step 2: Determine overall result

    Since both assertions pass, both tests pass successfully.
  3. Final Answer:

    Both tests pass -> Option D
  4. Quick Check:

    num+1 == expected for all inputs [OK]
Hint: Check each input pair separately for assertion [OK]
Common Mistakes:
  • Assuming one test fails without checking values
  • Confusing expected values with actual calculation
  • Ignoring that both inputs are tested
4. Identify the error in the following parameterized test code:
import pytest

@pytest.mark.parametrize('username, password')
def test_login(username, password):
    assert username != '' and password != ''
medium
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

  1. 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.
  2. Step 2: Verify other parts

    Function parameters match decorator names, assertion logic is valid, and pytest is imported correctly.
  3. Final Answer:

    Missing list of parameter values in @pytest.mark.parametrize decorator -> Option A
  4. 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

  1. 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.
  2. 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.
  3. 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
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard