0
0
PyTesttesting~5 mins

Parametrize with IDs in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
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.
Example:<br>
@pytest.mark.parametrize('num', [1, 2, 3], ids=lambda x: f"num_{x}")
def test_example(num):
    assert num > 0
<br>This names tests as num_1, num_2, num_3.
Click to reveal answer
What is the main purpose of the ids parameter in pytest.mark.parametrize?
ATo change test order
BTo skip tests
CTo provide custom names for test cases
DTo set test timeouts
Which of these is a valid way to use ids in pytest.mark.parametrize?
Aids=None
Bids=123
Cids=True
Dids=['case1', 'case2']
If you want dynamic test names based on parameter values, what should ids be?
AA boolean value
BA function that returns a string
CAn integer
DA list of integers
What happens if you do not provide ids in pytest.mark.parametrize?
ATests are named with default indexes like <code>[0]</code>, <code>[1]</code>
BTests will not run
CTests will fail automatically
DTests run in random order
Which of the following is NOT a benefit of using ids in parameterized tests?
AAutomatically fixes test bugs
BHelps identify failing test cases easily
CImproves test report readability
DAllows descriptive test case names
Explain how to use the ids parameter in pytest.mark.parametrize and why it is useful.
Think about how test names appear in reports.
You got /4 concepts.
    Describe a scenario where using a function for ids in parameterized tests is better than using a list of strings.
    Consider large or changing test data.
    You got /4 concepts.