Bird
Raised Fist0
PyTesttesting~10 mins

@pytest.mark.parametrize decorator - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to parametrize the test function with two values for x.

PyTest
import pytest

@pytest.mark.parametrize('x', [[1]])
def test_example(x):
    assert x > 0
Drag options to blanks, or click blank then click option'
A1 2
B1, 2
C(1, 2)
D'1, 2'
Attempts:
3 left
💡 Hint
Common Mistakes
Using space instead of comma to separate values
Passing a tuple instead of a list
Passing a string instead of a list
2fill in blank
medium

Complete the code to parametrize the test function with two parameters: x and y.

PyTest
import pytest

@pytest.mark.parametrize([1], [(1, 2), (3, 4)])
def test_sum(x, y):
    assert x + y > 0
Drag options to blanks, or click blank then click option'
A'(x, y)'
B['x', 'y']
C'x, y'
D('x', 'y')
Attempts:
3 left
💡 Hint
Common Mistakes
Passing parameter names as a list instead of a string
Using parentheses instead of quotes
Including spaces inside the string incorrectly
3fill in blank
hard

Fix the error in the parametrize decorator to correctly test multiple sets of inputs.

PyTest
import pytest

@pytest.mark.parametrize('x, y', [1])
def test_multiply(x, y):
    assert x * y >= 0
Drag options to blanks, or click blank then click option'
A[(1, 2), (3,)]
B[1, 2, 3, 4]
C[(1, 2), 3, 4]
D[(1, 2), (3, 4)]
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a flat list instead of list of tuples
Tuples with missing elements
Mixing tuples and single values
4fill in blank
hard

Fill both blanks to parametrize the test with three sets of inputs and check the sum.

PyTest
import pytest

@pytest.mark.parametrize([1], [2])
def test_sum(x, y):
    assert x + y == 3
Drag options to blanks, or click blank then click option'
A'x, y'
B[(1, 2), (2, 1), (0, 3)]
C[(1, 2), (2, 2), (0, 3)]
D'x,y'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect parameter string format
Using tuples with wrong sums
Using lists instead of tuples for values
5fill in blank
hard

Fill all three blanks to parametrize the test with parameters a, b and expected, and check the multiplication result.

PyTest
import pytest

@pytest.mark.parametrize([1], [2])
def test_multiply(a, b, expected):
    assert a * b == [3]
Drag options to blanks, or click blank then click option'
A'a, b, expected'
B[(2, 3, 6), (4, 5, 20), (1, 0, 0)]
Cexpected
D'a, b'
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching parameter names and values
Using wrong variable in assertion
Incorrect tuple lengths in values

Practice

(1/5)
1. What is the main purpose of the @pytest.mark.parametrize decorator in pytest?
easy
A. To skip a test function during test execution
B. To run the same test function multiple times with different input values
C. To mark a test as expected to fail
D. To group multiple test functions into a test suite

Solution

  1. Step 1: Understand the decorator's role

    @pytest.mark.parametrize allows running one test function multiple times with different inputs.
  2. Step 2: Compare options with this role

    Only To run the same test function multiple times with different input values describes running the same test with different inputs, matching the decorator's purpose.
  3. Final Answer:

    To run the same test function multiple times with different input values -> Option B
  4. Quick Check:

    Parametrize = multiple inputs [OK]
Hint: Parametrize means run test many times with different data [OK]
Common Mistakes:
  • Confusing parametrize with skipping tests
  • Thinking it groups tests instead of repeating one
  • Mixing it up with marking expected failures
2. Which of the following is the correct syntax to use @pytest.mark.parametrize for testing a function with inputs 1 and 2?
easy
A. @pytest.mark.parametrized('input', [1, 2])
B. @pytest.parametrize('input', (1, 2))
C. @pytest.mark.parametrize(input, [1, 2])
D. @pytest.mark.parametrize('input', [1, 2])

Solution

  1. Step 1: Check correct decorator name and syntax

    The correct decorator is @pytest.mark.parametrize with parameters as strings and list of values.
  2. Step 2: Validate each option

    @pytest.mark.parametrize('input', [1, 2]) uses correct spelling, quotes around parameter name, and list syntax. Others have syntax errors or wrong names.
  3. Final Answer:

    @pytest.mark.parametrize('input', [1, 2]) -> Option D
  4. Quick Check:

    Correct spelling and syntax = @pytest.mark.parametrize('input', [1, 2]) [OK]
Hint: Decorator name is mark.parametrize with string param name [OK]
Common Mistakes:
  • Misspelling 'parametrize' as 'parametrized'
  • Forgetting quotes around parameter name
  • Using wrong brackets or missing @ symbol
3. What will be the output when running this pytest code?
import pytest

@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
def test_sum(x, y):
    assert x + y == 3
medium
A. First test passes, second test fails
B. Both tests fail
C. Both tests pass
D. Syntax error during test collection

Solution

  1. Step 1: Analyze test cases and assertion

    Test runs twice: (1,2) and (3,4). Assertion checks if sum equals 3.
  2. Step 2: Evaluate each case

    1+2=3 passes; 3+4=7 fails assertion.
  3. Final Answer:

    First test passes, second test fails -> Option A
  4. Quick Check:

    Sum check true only for (1,2) [OK]
Hint: Check each input pair sum against assertion [OK]
Common Mistakes:
  • Assuming all tests pass without checking values
  • Confusing tuple unpacking in parametrize
  • Ignoring assertion logic
4. Identify the error in this pytest code using @pytest.mark.parametrize:
@pytest.mark.parametrize('num', [1, 2, 3])
def test_even():
    assert num % 2 == 0
medium
A. Wrong decorator name
B. List of values should be a tuple
C. Missing parameter in test function definition
D. Assertion logic is incorrect

Solution

  1. Step 1: Check function parameters

    Test function must accept the parameter named in parametrize ('num'), but test_even() has no parameters.
  2. Step 2: Confirm other parts

    Decorator name and list syntax are correct; assertion logic is valid for even check.
  3. Final Answer:

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

    Function must accept parametrize variables [OK]
Hint: Test function needs parameters matching parametrize names [OK]
Common Mistakes:
  • Forgetting to add parameters to test function
  • Thinking list must be tuple
  • Confusing assertion logic with syntax error
5. You want to test a function is_valid_age(age) that returns True if age is between 18 and 65 inclusive. Which @pytest.mark.parametrize usage correctly tests ages 17, 18, 30, 65, and 70 with expected results?
hard
A. @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)])
B. @pytest.mark.parametrize('age', [17, 18, 30, 65, 70], 'expected', [False, True, True, True, False])
C. @pytest.mark.parametrize('age,expected', {17: False, 18: True, 30: True, 65: True, 70: False})
D. @pytest.mark.parametrize(age,expected, [(17, False), (18, True), (30, True), (65, True), (70, False)])

Solution

  1. Step 1: Understand parametrize syntax for multiple parameters

    When passing multiple parameters, use a string with comma-separated names or a list of strings as first argument, and a list of tuples as second.
  2. Step 2: Evaluate each option

    A: @pytest.mark.parametrize(age,expected, [(17, False), (18, True), (30, True), (65, True), (70, False)]) misses quotes around parameter names, causing error. B has wrong syntax with multiple arguments. C uses a dict, which is invalid. D: @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) uses list of strings correctly.
  3. Step 3: Choose best practice

    @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) is correct and clear syntax for multiple parameters.
  4. Final Answer:

    @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) -> Option A
  5. Quick Check:

    List of param names + list of tuples = @pytest.mark.parametrize(['age', 'expected'], [(17, False), (18, True), (30, True), (65, True), (70, False)]) [OK]
Hint: Use list of param names and list of tuples for multiple inputs [OK]
Common Mistakes:
  • Passing multiple parameter lists separately
  • Using dict instead of list of tuples
  • Confusing string vs list for parameter names
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