Bird
Raised Fist0
Selenium Pythontesting~5 mins

Parameterize with test data in Selenium Python - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
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
Which Python decorator is commonly used to parameterize tests in pytest?
A@pytest.mark.parametrize
B@pytest.testdata
C@pytest.run
D@pytest.input
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
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
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
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

      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