0
0
PyTesttesting~20 mins

Fixture dependencies (fixture using fixture) in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Dependency Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest fixture dependency execution order
Given the following pytest code with fixtures depending on each other, what will be the output when running the test?
PyTest
import pytest

@pytest.fixture
def fixture_a():
    print('Setup A')
    yield 'A'
    print('Teardown A')

@pytest.fixture
def fixture_b(fixture_a):
    print('Setup B')
    yield f'B uses {fixture_a}'
    print('Teardown B')

@pytest.fixture
def fixture_c(fixture_b):
    print('Setup C')
    yield f'C uses {fixture_b}'
    print('Teardown C')

def test_example(fixture_c):
    print(f'Running test with {fixture_c}')
A
Setup B
Setup A
Setup C
Running test with C uses B uses A
Teardown A
Teardown C
Teardown B
B
Setup C
Setup B
Setup A
Running test with C uses B uses A
Teardown A
Teardown B
Teardown C
C
Setup A
Setup C
Setup B
Running test with C uses B uses A
Teardown B
Teardown C
Teardown A
D
Setup A
Setup B
Setup C
Running test with C uses B uses A
Teardown C
Teardown B
Teardown A
Attempts:
2 left
💡 Hint
Remember that pytest runs fixtures in dependency order: the deepest dependency first, then upwards.
assertion
intermediate
1:30remaining
Correct assertion for fixture dependency value
Given these fixtures, which assertion correctly verifies that the test receives the expected combined fixture value?
PyTest
import pytest

@pytest.fixture
def base():
    return 5

@pytest.fixture
def dependent(base):
    return base * 2

def test_value(dependent):
    # Which assertion is correct here?
Aassert dependent == 7
Bassert dependent == 5
Cassert dependent == 10
Dassert dependent == 0
Attempts:
2 left
💡 Hint
The dependent fixture doubles the base fixture value.
🔧 Debug
advanced
1:30remaining
Identify the error in fixture dependency usage
What error will occur when running this pytest code?
PyTest
import pytest

@pytest.fixture
def fix1():
    return 1

@pytest.fixture
def fix2():
    return 2

def test_error(fix1, fix3):
    assert fix1 + fix3 == 3
ATypeError: unsupported operand type(s) for +: 'int' and 'NoneType'
Bpytest fixture lookup error: fixture 'fix3' not found
CNameError: name 'fix3' is not defined
DTest passes with no error
Attempts:
2 left
💡 Hint
Check if all fixtures used in the test are defined.
framework
advanced
1:30remaining
Best practice for fixture dependency scope
If fixture B depends on fixture A, and fixture A has scope='module', what is the best scope to assign to fixture B to avoid scope mismatch errors?
Ascope='module'
Bscope='function'
Cscope='session'
Dscope='class'
Attempts:
2 left
💡 Hint
Fixture scope must be the same or broader than its dependencies.
🧠 Conceptual
expert
2:00remaining
Why use fixture dependencies in pytest?
Which of the following is the main advantage of using fixture dependencies (fixtures using other fixtures) in pytest?
ATo reduce code duplication by reusing setup logic across multiple fixtures
BTo make tests run faster by skipping setup steps
CTo avoid writing any teardown code
DTo force tests to run in a specific order
Attempts:
2 left
💡 Hint
Think about how fixtures can share common setup steps.