0
0
PyTesttesting~20 mins

@pytest.mark.parametrize decorator - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Parametrize Pro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a simple parametrize test
What will be the output when running this pytest test with @pytest.mark.parametrize?
PyTest
import pytest

@pytest.mark.parametrize('x,y', [(1,2), (3,4)])
def test_sum(x, y):
    assert x + y == 3
AOne test passes, one test fails
BBoth tests pass
CBoth tests fail
DSyntaxError due to parametrize usage
Attempts:
2 left
💡 Hint
Check the sum of each pair and compare with 3
assertion
intermediate
2:00remaining
Correct assertion for parametrize test
Which assertion correctly tests that the product of x and y equals 6 in this parametrize test?
PyTest
import pytest

@pytest.mark.parametrize('x,y', [(1,6), (2,3), (3,2)])
def test_product(x, y):
    # Choose the correct assertion below
Aassert x * y == 6
Bassert x + y == 6
Cassert x - y == 6
Dassert x / y == 6
Attempts:
2 left
💡 Hint
Product means multiplication
🔧 Debug
advanced
2:00remaining
Identify the error in parametrize usage
What error will this pytest code raise when run?
PyTest
import pytest

@pytest.mark.parametrize('x,y', [(1, 2), (3, 4)])
def test_values(x, y):
    assert x < y
ATypeError: cannot unpack non-iterable int object
BNo error, test runs successfully
CAssertionError: test fails
DSyntaxError: invalid syntax in parametrize decorator
Attempts:
2 left
💡 Hint
Check the format of the list passed to parametrize
🧠 Conceptual
advanced
2:00remaining
Purpose of @pytest.mark.parametrize
What is the main purpose of using @pytest.mark.parametrize in pytest tests?
ATo mark tests as expected to fail
BTo skip tests conditionally based on input
CTo run the same test function multiple times with different input values
DTo group multiple tests into one test suite
Attempts:
2 left
💡 Hint
Think about repeating tests with different data
framework
expert
2:00remaining
Combining parametrize with fixtures
Given this pytest code, what will be the output when running the test?
PyTest
import pytest

@pytest.fixture
def base_value():
    return 10

@pytest.mark.parametrize('increment', [1, 2])
def test_increment(base_value, increment):
    assert base_value + increment in [11, 12]
AFixture not found error
BFirst test passes, second test fails
CBoth tests fail
DBoth tests pass
Attempts:
2 left
💡 Hint
Check the sum of base_value and increment for each param