Bird
Raised Fist0
PyTesttesting~20 mins

Parametrize with IDs in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parametrize with IDs Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pytest parametrize with custom IDs

What will be the output when running this pytest test with the given parametrize and custom IDs?

PyTest
import pytest

@pytest.mark.parametrize("input,expected", [(2, 4), (3, 9), (4, 16)], ids=["two", "three", "four"])
def test_square(input, expected):
    assert input * input == expected

if __name__ == "__main__":
    import sys
    import pytest
    sys.exit(pytest.main([__file__, "-q", "--tb=line"]))
A3 passed (parametrize) in 0.01s
B3 passed in 0.01s
C3 passed (two, three, four) in 0.01s
D3 failed (two, three, four) in 0.01s
Attempts:
2 left
💡 Hint

Look at how the ids parameter affects the test report output.

❓ assertion
intermediate
2:00remaining
Correct assertion for parametrize with IDs

Given this pytest parametrize with IDs, which assertion correctly checks the test ID in a hook?

PyTest
import pytest

@pytest.mark.parametrize("num,expected", [(1, 1), (2, 4)], ids=["one", "two"])
def test_square(num, expected):
    assert num * num == expected

# Hook to check test ID
@pytest.hookimpl(tryfirst=True)
def pytest_runtest_setup(item):
    # Which assertion is correct here?
    pass
Aassert item.callspec.id == "one" or item.callspec.id == "two"
Bassert item.name == "test_square[one]" or item.name == "test_square[two]"
Cassert item.callspec.params == {"num": 1, "expected": 1}
Dassert item.callspec.id in ["one", "two"]
Attempts:
2 left
💡 Hint

Check how to access the test ID string from the item object.

🔧 Debug
advanced
2:00remaining
Debug why custom IDs are not shown in pytest report

Why does this pytest parametrize code not show the custom IDs in the test report?

PyTest
import pytest

@pytest.mark.parametrize("x,y", [(1,2), (3,4)], ids=lambda val: f"val_{val}")
def test_sum(x, y):
    assert x + y > 0
ABecause the ids function receives each parameter tuple, not individual values
BBecause the ids function must return a list, not a lambda
CBecause the ids function receives the whole tuple, not individual values
DBecause the ids parameter only accepts strings, not functions
Attempts:
2 left
💡 Hint

Check the expected input type for the ids callable in parametrize.

🧠 Conceptual
advanced
2:00remaining
Purpose of IDs in pytest parametrize

What is the main purpose of using the ids parameter in pytest.mark.parametrize?

ATo change the order in which tests run
BTo provide custom names for each test case for better readability in reports
CTo skip certain test cases based on their ID
DTo group tests into categories for parallel execution
Attempts:
2 left
💡 Hint

Think about how test reports display parameterized tests.

❓ framework
expert
3:00remaining
Implementing dynamic IDs for complex parameters in pytest

You have a list of dictionaries as parameters for a pytest test. How do you implement ids to generate readable test IDs showing the 'name' key from each dictionary?

PyTest
import pytest

params = [
    {"name": "alpha", "value": 1},
    {"name": "beta", "value": 2},
    {"name": "gamma", "value": 3}
]

@pytest.mark.parametrize("param", params, ids=??? )
def test_value(param):
    assert param["value"] > 0
Aids=[d["name"] for d in params]
Bids=[str(d["value"]) for d in params]
Cids=lambda d: str(d)
Dids=lambda d: d["name"]
Attempts:
2 left
💡 Hint

Remember that ids can be a list of strings or a callable.

Practice

(1/5)
1. What is the main purpose of using ids in @pytest.mark.parametrize?
easy
A. To change the order of test execution
B. To skip certain test cases during execution
C. To label each test case for easier identification in reports
D. To automatically generate test data

Solution

  1. Step 1: Understand the role of ids in parametrize

    The ids parameter assigns names to each test case, making it easier to identify which input caused a failure.
  2. Step 2: Compare with other options

    Skipping tests, changing order, or generating data are not functions of ids.
  3. Final Answer:

    To label each test case for easier identification in reports -> Option C
  4. Quick Check:

    Parametrize ids = labels [OK]
Hint: IDs name test cases for clear failure reports [OK]
Common Mistakes:
  • Thinking IDs skip tests
  • Believing IDs reorder tests
  • Assuming IDs generate data
2. Which of the following is the correct syntax to add IDs to a parametrize decorator in pytest?
easy
A. @pytest.mark.parametrize('x,y', [(1,2), (3,4)], ids=['first', 'second'])
B. @pytest.mark.parametrize('x,y', [(1,2), (3,4)], id=['first', 'second'])
C. @pytest.mark.parametrize('x,y', [(1,2), (3,4)], names=['first', 'second'])
D. @pytest.mark.parametrize('x,y', [(1,2), (3,4)], labels=['first', 'second'])

Solution

  1. Step 1: Identify the correct parameter name

    The correct parameter to assign IDs is ids, not id, names, or labels.
  2. Step 2: Check the syntax structure

    The syntax uses @pytest.mark.parametrize with the argument names, list of tuples, and ids as a list of strings.
  3. Final Answer:

    @pytest.mark.parametrize('x,y', [(1,2), (3,4)], ids=['first', 'second']) -> Option A
  4. Quick Check:

    Use ids parameter correctly [OK]
Hint: Use 'ids' keyword exactly for naming test cases [OK]
Common Mistakes:
  • Using 'id' instead of 'ids'
  • Using 'names' or 'labels' which are invalid
  • Forgetting to pass a list for IDs
3. Given the code below, what will be the test ID shown for the second test case?
@pytest.mark.parametrize('num', [10, 20], ids=['ten', 'twenty'])
def test_value(num):
    assert num > 5
medium
A. test_value[twenty]
B. test_value[10]
C. test_value[1]
D. test_value[20]

Solution

  1. Step 1: Understand how IDs map to test cases

    The ids list assigns 'ten' to the first input (10) and 'twenty' to the second input (20).
  2. Step 2: Identify the second test case ID

    The second test case uses the second ID, which is 'twenty'.
  3. Final Answer:

    test_value[twenty] -> Option A
  4. Quick Check:

    Second ID label = 'twenty' [OK]
Hint: IDs replace values in test names [OK]
Common Mistakes:
  • Assuming default numeric index is used
  • Confusing value with ID label
  • Ignoring the IDs parameter
4. Identify the error in the following pytest parametrize usage:
@pytest.mark.parametrize('a,b', [(1,2), (3,4)], ids='case1,case2')
def test_sum(a, b):
    assert a + b > 0
medium
A. Tuple values must be strings
B. Parameter names must be a list, not a string
C. Test function must return a value
D. IDs should be a list, not a single string

Solution

  1. Step 1: Check the type of ids argument

    The ids parameter expects a list of strings, but here it is given a single string with commas.
  2. Step 2: Understand the impact of wrong ids type

    Passing a string instead of a list causes pytest to treat it as an iterable of characters, leading to incorrect IDs or errors.
  3. Final Answer:

    IDs should be a list, not a single string -> Option D
  4. Quick Check:

    IDs must be list of strings [OK]
Hint: Pass IDs as list, not comma string [OK]
Common Mistakes:
  • Passing comma-separated string instead of list
  • Using wrong parameter type for IDs
  • Confusing parameter names with IDs
5. You want to parametrize a test with inputs [(0, 'zero'), (1, 'one'), (2, 'two')] but want to skip the test case where the number is zero using IDs. Which approach correctly uses ids to help identify the skipped test case?
hard
A. @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=lambda val: f"skip_{val[0]}" if val[0]==0 else f"num_{val[0]}")
B. @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two'])
C. @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['zero', 'one', 'two'])
D. @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['num_0', 'num_1', 'num_2'])

Solution

  1. Step 1: Understand the goal of IDs for skipping

    We want to label the zero case clearly as 'skip_zero' to identify it easily in reports.
  2. Step 2: Evaluate each option's IDs usage

    @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two']) uses a list of strings with 'skip_zero' for zero, which is clear and correct. The lambda in @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=lambda val: f"skip_{val[0]}" if val[0]==0 else f"num_{val[0]}") is corrected to accept a single tuple argument as pytest passes. The options with ids=['num_0', 'num_1', 'num_2'] and ids=['zero', 'one', 'two'] do not label zero as skipped.
  3. Final Answer:

    @pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two']) -> Option B
  4. Quick Check:

    Use explicit ID list to mark skipped case [OK]
Hint: Use explicit ID list to label skipped cases [OK]
Common Mistakes:
  • Using lambda incorrectly for IDs
  • Not labeling skipped cases clearly
  • Using generic IDs without skip info
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