Recall & Review
beginner
What is a shared expensive resource in pytest?
A shared expensive resource is a setup or object that takes a lot of time or resources to create, so it is created once and reused across multiple tests to save time.
Click to reveal answer
beginner
How does pytest's
@pytest.fixture(scope='session') help with shared expensive resources?It creates the resource once per test session and shares it with all tests, avoiding repeated expensive setup.
Click to reveal answer
intermediate
Why should you avoid using
scope='module' for very expensive resources in pytest?Because if tests run in parallel or across multiple modules, the resource might be recreated multiple times, losing the benefit of sharing.
Click to reveal answer
intermediate
What is the purpose of the
yield statement in a pytest fixture for shared resources?It allows setup before
yield and cleanup after, ensuring the expensive resource is properly released after all tests use it.Click to reveal answer
advanced
How can you ensure thread safety when sharing an expensive resource in pytest?
By using locks or synchronization inside the fixture or resource to prevent conflicts when tests run in parallel.
Click to reveal answer
Which pytest fixture scope is best for sharing a resource across all tests in a session?
✗ Incorrect
The 'session' scope creates the fixture once per test session, ideal for expensive shared resources.
What does the
yield keyword do in a pytest fixture?✗ Incorrect
Yield allows setup before tests run and cleanup after tests finish using the fixture.
Why is sharing expensive resources beneficial in testing?
✗ Incorrect
Sharing expensive resources saves time by creating them once and reusing them.
If tests run in parallel, what must you consider when sharing resources?
✗ Incorrect
Parallel tests need thread-safe shared resources to avoid conflicts.
Which fixture scope creates a new resource for every test function?
✗ Incorrect
The 'function' scope creates a fresh fixture for each test function.
Explain how to create and use a shared expensive resource in pytest using fixtures.
Think about how to write a fixture that runs once and cleans up after all tests.
You got /5 concepts.
Describe challenges and solutions when sharing expensive resources in parallel pytest runs.
Consider what happens if multiple tests access the same resource at the same time.
You got /5 concepts.