0
0
PyTesttesting~20 mins

Context manager fixtures in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Context Manager Fixture Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a pytest fixture using context manager
What will be the output when running this pytest test with the given fixture?
PyTest
import pytest

class Resource:
    def __init__(self):
        self.active = False
    def __enter__(self):
        self.active = True
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.active = False

@pytest.fixture
def resource():
    with Resource() as r:
        yield r

def test_resource_active(resource):
    assert resource.active
ATest passes because resource.active is True inside the test
BTest fails because resource.active is False inside the test
CTest fails due to fixture not yielding any value
DTest raises an exception because Resource does not support context manager
Attempts:
2 left
💡 Hint
Remember that the fixture yields the resource while it is active inside the context manager.
assertion
intermediate
2:00remaining
Correct assertion to check resource active state in context manager fixture
Given this fixture and test, which assertion correctly verifies the state of the resource during the test?
PyTest
import pytest

class Resource:
    def __init__(self):
        self.active = False
    def __enter__(self):
        self.active = True
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        self.active = False

@pytest.fixture
def resource():
    with Resource() as r:
        yield r

def test_resource_cleanup(resource):
    pass  # Which assertion goes here?
Aassert hasattr(resource, 'active')
Bassert resource.active is False
Cassert resource.active is True
Dassert resource is None
Attempts:
2 left
💡 Hint
The test runs while the resource is active inside the fixture yield.
🔧 Debug
advanced
2:00remaining
Common misconception: Why does this context manager fixture seem to cause a test to hang?
Consider this fixture and test. Some developers think the test never finishes. What is the most likely reason for this misconception?
PyTest
import pytest

class Resource:
    def __enter__(self):
        print('Entering')
        return self
    def __exit__(self, exc_type, exc_val, exc_tb):
        print('Exiting')

@pytest.fixture
def resource():
    with Resource() as r:
        yield r
        print('After yield')

def test_hang(resource):
    assert True
AThe fixture never exits the with block because __exit__ is missing a return statement
BThe fixture hangs because the yield is inside the with block and the with block never ends
CThe fixture hangs because __exit__ does not return True to suppress exceptions
DThe fixture hangs because the test does not consume the fixture properly
Attempts:
2 left
💡 Hint
Think about when the with block ends relative to the yield.
locator
advanced
2:00remaining
Best locator strategy for a pytest fixture using context manager
You want to locate the line in this fixture where the resource is yielded to the test. Which line number is the best locator?
PyTest
1| import pytest
2| 
3| class Resource:
4|     def __enter__(self):
5|         print('Enter')
6|         return self
7|     def __exit__(self, exc_type, exc_val, exc_tb):
8|         print('Exit')
9| 
10| @pytest.fixture
11| def resource():
12|     with Resource() as r:
13|         yield r
14|         print('Cleanup')
ALine 13: yield r
BLine 14: print('Cleanup')
CLine 11: def resource():
DLine 12: with Resource() as r:
Attempts:
2 left
💡 Hint
The yield statement is where the fixture hands control to the test.
framework
expert
2:00remaining
How does pytest handle teardown in context manager fixtures?
In pytest, when a fixture uses a context manager with yield, when exactly does pytest execute the code after the yield?
APytest runs the code after yield only if the test fails
BBefore the test function starts, pytest runs the code after yield to prepare the environment
CPytest never runs the code after yield in context manager fixtures
DImmediately after the test function finishes, pytest resumes the fixture and runs the code after yield for teardown
Attempts:
2 left
💡 Hint
Think about how yield pauses and resumes execution.