0
0
PyTesttesting~10 mins

Parametrize with IDs in PyTest - Interactive Code Practice

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

Complete the code to parametrize a test with two values.

PyTest
import pytest

@pytest.mark.parametrize('num', [1])
def test_is_positive(num):
    assert num > 0
Drag options to blanks, or click blank then click option'
A[1, 2]
B(1, 2)
C{1, 2}
D'1, 2'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a list or tuple.
Using a set which is unordered and not recommended here.
2fill in blank
medium

Complete the code to parametrize a test with two arguments and IDs.

PyTest
import pytest

@pytest.mark.parametrize(
    'input,expected',
    [(2, 4), (3, 9)],
    ids=[1]
)
def test_square(input, expected):
    assert input ** 2 == expected
Drag options to blanks, or click blank then click option'
A['two', 'three']
B['2', '3']
C['square2', 'square3']
D['input2', 'input3']
Attempts:
3 left
💡 Hint
Common Mistakes
Using numbers instead of strings for IDs.
Using IDs list length different from test cases.
3fill in blank
hard

Fix the error in the parametrize decorator by completing the IDs argument correctly.

PyTest
import pytest

@pytest.mark.parametrize(
    'value',
    [10, 20, 30],
    ids=[1]
)
def test_greater_than_five(value):
    assert value > 5
Drag options to blanks, or click blank then click option'
A['10', '20']
B['10', '20', 30]
C['ten', 'twenty', 'thirty', 'forty']
D['10', '20', '30']
Attempts:
3 left
💡 Hint
Common Mistakes
Providing fewer or more IDs than test cases.
Mixing string and integer types in IDs.
4fill in blank
hard

Fill both blanks to parametrize a test with multiple arguments and custom IDs.

PyTest
import pytest

@pytest.mark.parametrize(
    [1],
    [(1, 'a'), (2, 'b')],
    ids=[2]
)
def test_multiple_args(num, char):
    assert isinstance(num, int) and isinstance(char, str)
Drag options to blanks, or click blank then click option'
A'num, char'
B'number, letter'
C['case1', 'case2']
D['first', 'second']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a string for argument names.
Mismatch between number of IDs and test cases.
5fill in blank
hard

Fill all three blanks to parametrize a test with three arguments, test data, and custom IDs.

PyTest
import pytest

@pytest.mark.parametrize(
    [1],
    [2],
    ids=[3]
)
def test_three_args(x, y, z):
    assert x < y < z
Drag options to blanks, or click blank then click option'
A'x, y, z'
B[(1, 2, 3), (4, 5, 6)]
C['first', 'second']
D['a', 'b']
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect data types for arguments or test data.
Mismatch in length between test data and IDs.