Challenge - 5 Problems
Conftest Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of shared fixture usage across test files
Given the following conftest.py fixture and two test files, what will be the output when running all tests?
PyTest
## conftest.py import pytest @pytest.fixture(scope='module') def resource(): print('Setup resource') yield 'resource_data' print('Teardown resource') ## test_file1.py def test_one(resource): assert resource == 'resource_data' ## test_file2.py def test_two(resource): assert resource == 'resource_data'
Attempts:
2 left
💡 Hint
Think about the scope='module' fixture and how many times it runs across multiple test files.
✗ Incorrect
A fixture with scope='module' runs once per test module. Since conftest.py is shared, and there are two separate test files (modules), the fixture runs once per test file: setup and teardown for test_file1.py, then for test_file2.py. The output is Setup resource\nTeardown resource\nSetup resource\nTeardown resource.
❓ assertion
intermediate1:30remaining
Correct assertion for fixture data modification
A conftest fixture returns a list. A test modifies the list by appending an item. Which assertion correctly verifies the list content inside the test?
PyTest
import pytest @pytest.fixture def sample_list(): return [1, 2, 3] def test_append(sample_list): sample_list.append(4) # Which assertion is correct here?
Attempts:
2 left
💡 Hint
Remember how list append works and how to compare lists in Python.
✗ Incorrect
The append method adds the element at the end of the list, so the list becomes [1, 2, 3, 4]. The correct way to check list equality is using '=='. Using 'is' checks identity, which is not correct here. The order matters, so [4, 1, 2, 3] is incorrect.
🔧 Debug
advanced2:00remaining
Identify the cause of fixture not being shared
You have a fixture in conftest.py with scope='session', but it seems to run before every test function instead of once per session. What is the most likely cause?
PyTest
## conftest.py import pytest @pytest.fixture(scope='session') def db_connection(): print('Connecting to DB') yield 'db_conn' print('Closing DB') ## test_module.py def test_a(db_connection): assert db_connection == 'db_conn' def test_b(db_connection): assert db_connection == 'db_conn'
Attempts:
2 left
💡 Hint
Think about how pytest runs tests and how filtering affects fixture scope.
✗ Incorrect
When pytest runs tests with '-k' or other filters that select individual tests, it may create separate sessions for each run, causing session-scoped fixtures to run multiple times. The fixture is correctly defined and shared, but the test run method causes multiple runs.
❓ framework
advanced1:30remaining
Best practice for fixture sharing in large projects
In a large pytest project with many test files, where should you place fixtures that need to be shared across multiple test files?
Attempts:
2 left
💡 Hint
Think about pytest's automatic fixture discovery.
✗ Incorrect
Pytest automatically discovers fixtures defined in conftest.py files in the directory tree. Placing shared fixtures in conftest.py at a common root allows all tests below to use them without imports. Defining fixtures inside test files or classes limits sharing. Manual imports are possible but less convenient.
🧠 Conceptual
expert2:00remaining
Understanding fixture scope and autouse behavior
Consider a fixture in conftest.py defined with scope='function' and autouse=True. How many times will this fixture run if you have 3 test functions in 2 different test files?
Attempts:
2 left
💡 Hint
Remember what scope='function' and autouse=True mean for fixture execution.
✗ Incorrect
A fixture with scope='function' runs before each test function. Autouse=True means it runs automatically without needing to be requested. There are 3 test functions total across the 2 files, so it runs 3 times.