0
0
PyTesttesting~20 mins

Handling shared resources 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 shared resource
Consider the following pytest code using a fixture that manages a shared resource. What will be the output when running the test?
PyTest
import pytest

shared_list = []

@pytest.fixture(scope="module")
def resource():
    shared_list.append(1)
    yield shared_list
    shared_list.clear()

def test_one(resource):
    resource.append(2)
    assert resource == [1, 2]

def test_two(resource):
    resource.append(3)
    assert resource == [1, 3]
ABoth test_one and test_two fail with AssertionError
BBoth test_one and test_two pass
Ctest_one fails with AssertionError, test_two passes
Dtest_one passes, test_two fails with AssertionError
Attempts:
2 left
💡 Hint
Think about how the shared_list is modified and when it is cleared.
assertion
intermediate
1:30remaining
Correct assertion for shared file content
You have a shared file resource used in multiple tests. Which assertion correctly verifies that the file content includes the string 'success' after writing?
PyTest
def test_write_file(shared_file):
    shared_file.write('operation success')
    shared_file.seek(0)
    content = shared_file.read()
Aassert 'success' in content
Bassert content == 'success'
Cassert content.startswith('success')
Dassert content.endswith('success')
Attempts:
2 left
💡 Hint
The file content may have other text besides 'success'.
🔧 Debug
advanced
2:30remaining
Identify the cause of test interference with shared database connection
Two tests share a database connection fixture with function scope. Test A inserts a row but does not rollback. Test B expects an empty table but fails. What is the most likely cause?
PyTest
import pytest

@pytest.fixture(scope='function')
def db_connection():
    conn = create_connection()
    yield conn
    conn.close()

def test_a(db_connection):
    db_connection.execute('INSERT INTO users VALUES (1, "Alice")')
    # missing rollback

def test_b(db_connection):
    result = db_connection.execute('SELECT * FROM users')
    assert len(result.fetchall()) == 0
ATest A did not rollback, so data persists and affects Test B
BThe fixture scope should be module, not function
CTest B has a wrong assertion expecting empty table
DThe connection is closed too early in Test A
Attempts:
2 left
💡 Hint
Think about transaction handling and test isolation.
framework
advanced
1:30remaining
Best pytest fixture scope for expensive shared resource
You have a resource that takes 10 minutes to set up and can be safely shared across all tests in a module. Which pytest fixture scope is best to use?
Ascope='function' to isolate each test
Bscope='module' to share resource across all tests in the module
Cscope='session' to share resource across all test sessions
Dscope='class' to share resource across tests in a class
Attempts:
2 left
💡 Hint
Choose a scope that balances setup time and test isolation.
🧠 Conceptual
expert
3:00remaining
Handling flaky tests caused by shared resource contention
You notice intermittent test failures caused by multiple tests accessing a shared resource concurrently. What is the best approach to handle this in pytest?
AUse pytest-xdist to run tests in parallel to reduce test time
BMark flaky tests as expected failures to ignore them
CAdd locks or synchronization in fixtures to serialize access to the shared resource
DIncrease the fixture scope to session to share the resource more broadly
Attempts:
2 left
💡 Hint
Think about how to prevent concurrent access issues.