Bird
Raised Fist0
PyTesttesting~20 mins

Parametrized fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parametrized Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of a parametrized fixture with multiple values
What will be the output when running this pytest test with the given parametrized fixture?
PyTest
import pytest

@pytest.fixture(params=[1, 2, 3])
def number(request):
    return request.param

def test_is_positive(number):
    assert number > 0
    print(f"Testing with number: {number}")
ATest runs once printing: Testing with number: [1, 2, 3]
BTest runs 3 times printing: Testing with number: 1, Testing with number: 2, Testing with number: 3
CTest runs 3 times but fails on number 1
DTest runs 1 time and fails because number is not defined
Attempts:
2 left
💡 Hint
Remember that pytest runs the test once for each parameter in the fixture.
❓ assertion
intermediate
2:00remaining
Correct assertion for a parametrized fixture test
Given this parametrized fixture, which assertion correctly verifies the fixture value is even?
PyTest
import pytest

@pytest.fixture(params=[2, 4, 5])
def even_number(request):
    return request.param

def test_even(even_number):
    # Which assertion is correct here?
Aassert even_number % 2 == 0
Bassert even_number % 2 != 0
Cassert even_number == 5
Dassert even_number > 5
Attempts:
2 left
💡 Hint
An even number is divisible by 2 with no remainder.
🔧 Debug
advanced
2:00remaining
Identify the error in this parametrized fixture usage
What error will this pytest code raise when executed?
PyTest
import pytest

@pytest.fixture(params=["a", "b", "c"])
def letter(request):
    return letter

def test_letter(letter):
    assert letter in ['a', 'b', 'c']
AAssertionError because letter is not in the list
BTypeError because 'letter' is not iterable
CRecursionError due to fixture returning itself
DNo error, test passes
Attempts:
2 left
💡 Hint
Check what the fixture returns inside its function body.
❓ framework
advanced
2:00remaining
Using indirect parametrization with fixtures
Which option correctly uses indirect parametrization to pass parameters to a fixture?
PyTest
import pytest

@pytest.fixture
def data(request):
    return request.param * 2

def test_data(data):
    assert data in [2, 4, 6]
A
@pytest.mark.parametrize('data', [1, 2, 3])
def test_data(data):
    assert data in [2, 4, 6]
B
@pytest.mark.parametrize('data', [2, 4, 6], indirect=False)
def test_data(data):
    assert data in [2, 4, 6]
C
@pytest.mark.parametrize('request', [1, 2, 3], indirect=True)
def test_data(data):
    assert data in [2, 4, 6]
D
@pytest.mark.parametrize('data', [1, 2, 3], indirect=True)
def test_data(data):
    assert data in [2, 4, 6]
Attempts:
2 left
💡 Hint
Indirect parametrization tells pytest to pass parameters to the fixture, not directly to the test.
🧠 Conceptual
expert
2:00remaining
Why use parametrized fixtures instead of simple parametrized tests?
Which is the best explanation for using parametrized fixtures over parametrized tests in pytest?
AParametrized fixtures allow sharing setup code and parameters across multiple tests, improving code reuse and clarity.
BParametrized fixtures run tests faster by skipping setup steps.
CParametrized fixtures prevent tests from running multiple times.
DParametrized fixtures automatically generate test reports without assertions.
Attempts:
2 left
💡 Hint
Think about how fixtures help organize setup and data for tests.

Practice

(1/5)
1. What is the main purpose of using @pytest.fixture(params=[...]) in pytest?
easy
A. To run the same test multiple times with different input data
B. To skip a test conditionally
C. To mark a test as expected to fail
D. To group multiple tests into a single test case

Solution

  1. Step 1: Understand the role of parametrized fixtures

    Parametrized fixtures allow running a test function multiple times with different data inputs automatically.
  2. Step 2: Match the purpose with the options

    To run the same test multiple times with different input data correctly describes this behavior, while others describe unrelated pytest features.
  3. Final Answer:

    To run the same test multiple times with different input data -> Option A
  4. Quick Check:

    Parametrized fixtures = run test multiple times [OK]
Hint: Parametrized fixtures repeat tests with different data [OK]
Common Mistakes:
  • Confusing parametrized fixtures with test skipping
  • Thinking it groups tests instead of repeating them
  • Mixing up with expected failure markers
2. Which of the following is the correct syntax to define a parametrized fixture in pytest?
easy
A. @pytest.fixture(param=[1, 2, 3])\ndef data(request):\n return request.param
B. @pytest.param(params=[1, 2, 3])\ndef data(request):\n return request.param
C. @pytest.fixture(params=[1, 2, 3])\ndef data(request):\n return request.param
D. @pytest.fixture(params=(1, 2, 3))\ndef data():\n return param

Solution

  1. Step 1: Identify correct decorator and parameter name

    The correct decorator is @pytest.fixture with the keyword params as a list.
  2. Step 2: Check function signature and usage

    The fixture function must accept request and return request.param to access each parameter value.
  3. Final Answer:

    @pytest.fixture(params=[1, 2, 3])\ndef data(request):\n return request.param -> Option C
  4. Quick Check:

    Use @pytest.fixture(params=[...]) and request.param [OK]
Hint: Use @pytest.fixture(params=[...]) and request.param inside function [OK]
Common Mistakes:
  • Using @pytest.param instead of @pytest.fixture
  • Misspelling 'params' as 'param'
  • Not including 'request' parameter in fixture function
  • Returning undefined variable instead of request.param
3. Given the following pytest code, what will be the output when running the test?
import pytest

@pytest.fixture(params=[10, 20])
def number(request):
    return request.param

def test_double(number):
    assert number * 2 in [20, 40]
medium
A. First test fails, second test passes
B. Both tests fail
C. First test passes, second test fails
D. Both tests pass

Solution

  1. Step 1: Understand parametrized fixture values

    The fixture 'number' will run the test twice: once with 10, once with 20.
  2. Step 2: Evaluate the assertion for each value

    For 10: 10*2=20, which is in [20,40]. For 20: 20*2=40, also in [20,40]. Both assertions pass.
  3. Final Answer:

    Both tests pass -> Option D
  4. Quick Check:

    10*2=20 and 20*2=40 both in list [OK]
Hint: Check each param value and test assertion separately [OK]
Common Mistakes:
  • Assuming only one test runs
  • Misreading the assertion condition
  • Forgetting fixture runs test multiple times
4. Identify the error in this parametrized fixture code snippet:
@pytest.fixture(params=["a", "b"])
def letter():
    return param

def test_letter(letter):
    assert letter in ["a", "b"]
medium
A. Wrong assertion in test function
B. Missing 'request' parameter in fixture function
C. Incorrect decorator name
D. Fixture should not return any value

Solution

  1. Step 1: Check fixture function parameters

    The fixture uses params, so it must accept request to access request.param.
  2. Step 2: Identify incorrect return statement

    The function returns param, which is undefined. It should return request.param.
  3. Final Answer:

    Missing 'request' parameter in fixture function -> Option B
  4. Quick Check:

    Parametrized fixture needs 'request' param [OK]
Hint: Parametrized fixtures must accept 'request' to use request.param [OK]
Common Mistakes:
  • Not including 'request' parameter in fixture
  • Returning undefined variable instead of request.param
  • Confusing fixture decorator names
5. You want to test a function with multiple sets of user data, but some data sets are invalid and should be skipped. How can you modify a parametrized fixture to skip tests for invalid data?
hard
A. Use pytest.skip() inside the fixture when data is invalid
B. Remove invalid data from the params list before parametrizing
C. Raise an exception inside the test function for invalid data
D. Use pytest.mark.skipif on the test function with a condition

Solution

  1. Step 1: Understand skipping inside parametrized fixtures

    Inside the fixture, you can call pytest.skip() when the current parameter is invalid to skip that test run.
  2. Step 2: Compare with other options

    Removing invalid data beforehand avoids testing them but doesn't show skipped tests. Raising exceptions fails tests, not skip. Using skipif on test function is less flexible for per-parameter skipping.
  3. Final Answer:

    Use pytest.skip() inside the fixture when data is invalid -> Option A
  4. Quick Check:

    Call pytest.skip() in fixture to skip invalid params [OK]
Hint: Call pytest.skip() inside fixture for invalid params [OK]
Common Mistakes:
  • Removing invalid data silently instead of skipping
  • Raising exceptions instead of skipping
  • Using skipif without parameter-specific logic
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard