0
0
PyTesttesting~20 mins

Parametrize with IDs in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 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.