Challenge - 5 Problems
Shared Resource Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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]
Attempts:
2 left
💡 Hint
Think about how the shared_list is modified and when it is cleared.
✗ Incorrect
The fixture has module scope, so shared_list is initialized once with [1]. test_one appends 2, so resource is [1, 2]. test_two runs next and appends 3, so resource becomes [1, 2, 3]. The assertion in test_two expects [1, 3], so it fails.
❓ assertion
intermediate1: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()
Attempts:
2 left
💡 Hint
The file content may have other text besides 'success'.
✗ Incorrect
The content includes the word 'success' but also other text. Using 'in' checks for substring presence, which is correct here.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Think about transaction handling and test isolation.
✗ Incorrect
Since Test A inserts data but does not rollback, the inserted row remains in the database. Test B sees this data and fails its assertion expecting an empty table.
❓ framework
advanced1: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?
Attempts:
2 left
💡 Hint
Choose a scope that balances setup time and test isolation.
✗ Incorrect
Module scope creates the resource once per module, which is efficient and safe here. Function scope is too frequent, session scope may be too broad, and class scope depends on test organization.
🧠 Conceptual
expert3: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?
Attempts:
2 left
💡 Hint
Think about how to prevent concurrent access issues.
✗ Incorrect
Adding locks or synchronization ensures only one test accesses the shared resource at a time, preventing race conditions and flaky failures.