0
0
PyTesttesting~20 mins

Shared expensive resource patterns in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Shared Resource Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pytest fixture with module scope
What will be the output when running this pytest code with two test functions using a module-scoped fixture?
PyTest
import pytest

@pytest.fixture(scope="module")
def resource():
    print("Setup resource")
    yield "data"
    print("Teardown resource")

def test_one(resource):
    print(f"Test one got {resource}")
    assert resource == "data"

def test_two(resource):
    print(f"Test two got {resource}")
    assert resource == "data"
ASetup resource\nTeardown resource\nTest one got data\nTest two got data
BTest one got data\nTest two got data
CSetup resource\nTest one got data\nTest two got data\nTeardown resource
DSetup resource\nTest one got data\nTeardown resource\nSetup resource\nTest two got data\nTeardown resource
Attempts:
2 left
💡 Hint
Module scope means the fixture runs once per module, not per test.
assertion
intermediate
2:00remaining
Correct assertion for shared resource usage count
Given a shared resource fixture that counts how many times it was used, which assertion correctly verifies it was used exactly twice after two tests?
PyTest
import pytest

usage_count = 0

@pytest.fixture(scope="module")
def shared_resource():
    global usage_count
    usage_count += 1
    yield


def test_a(shared_resource):
    pass

def test_b(shared_resource):
    pass

# After tests run, which assertion is correct?
Aassert usage_count == 1
Bassert usage_count == 2
Cassert usage_count >= 2
Dassert usage_count == 0
Attempts:
2 left
💡 Hint
Remember the fixture scope affects how many times setup code runs.
🔧 Debug
advanced
2:00remaining
Identify the problem with fixture teardown order
This pytest code uses two fixtures: one expensive resource and one dependent on it. The teardown prints are not in expected order. What causes this?
PyTest
import pytest

@pytest.fixture(scope="module")
def expensive_resource():
    print("Setup expensive")
    yield
    print("Teardown expensive")

@pytest.fixture(scope="module")
def dependent_resource(expensive_resource):
    print("Setup dependent")
    yield
    print("Teardown dependent")

def test_example(dependent_resource):
    assert True
ATeardown order depends on test function order
BTeardown order is correct: expensive_resource teardown runs before dependent_resource teardown
CFixtures with same scope teardown in random order
DTeardown order is reversed: dependent_resource teardown runs before expensive_resource teardown
Attempts:
2 left
💡 Hint
Think about fixture dependency and teardown sequence.
framework
advanced
2:00remaining
Best way to share a database connection across tests
You want to share a single expensive database connection across multiple test files in pytest. Which fixture scope and method is best?
AUse a fixture with scope='function' that opens and closes connection per test
BUse a fixture with scope='session' that opens connection once and yields it
CUse a fixture with scope='module' but open connection in each test manually
DUse a fixture with scope='class' that opens connection once per test class
Attempts:
2 left
💡 Hint
Think about how often you want the expensive setup to run.
🧠 Conceptual
expert
2:00remaining
Why use autouse fixtures for shared expensive resources?
What is the main advantage of using an autouse fixture with an expensive resource in pytest?
AIt automatically provides the resource to all tests without needing explicit parameters
BIt runs the fixture setup multiple times per test to ensure freshness
CIt disables teardown to keep the resource alive after tests
DIt forces tests to fail if they do not use the resource
Attempts:
2 left
💡 Hint
Think about convenience and test code simplicity.