What does the ids parameter do in pytest.mark.parametrize?
The ids parameter provides custom names for each set of parameters. These names appear in test reports and help identify tests clearly.
Click to reveal answer
beginner
How do you use ids with a list of strings in pytest.mark.parametrize?
You pass a list of strings to ids, each string naming the corresponding parameter set. For example: @pytest.mark.parametrize('x,y', [(1,2), (3,4)], ids=['first', 'second']).
Click to reveal answer
beginner
Why is using ids helpful when running many parameterized tests?
Using ids makes test reports easier to read. Instead of seeing generic indexes, you see meaningful names that explain what each test case is about.
Click to reveal answer
intermediate
Can ids be a function in pytest.mark.parametrize? What does it do?
Yes, ids can be a function that takes a parameter set and returns a string. This lets you create dynamic, descriptive names for each test case.
Click to reveal answer
intermediate
Show a simple example of pytest.mark.parametrize using ids as a function.
The ids list assigns 'ten' to the first input (10) and 'twenty' to the second input (20).
Step 2: Identify the second test case ID
The second test case uses the second ID, which is 'twenty'.
Final Answer:
test_value[twenty] -> Option A
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
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.
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.
Final Answer:
IDs should be a list, not a single string -> Option D
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
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.
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.
Final Answer:
@pytest.mark.parametrize('num,word', [(0,'zero'),(1,'one'),(2,'two')], ids=['skip_zero', 'num_one', 'num_two']) -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.