0
0
PyTesttesting~20 mins

Fixture scope (function, class, module, session) in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fixture Scope 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 function scope
What will be the output count of the fixture usage in the following pytest code?
PyTest
import pytest

count = 0

@pytest.fixture(scope='function')
def my_fixture():
    global count
    count += 1
    return count

def test_one(my_fixture):
    assert my_fixture == 1

def test_two(my_fixture):
    assert my_fixture == 2
ABoth tests fail due to fixture scope error
BBoth tests pass; count remains 1
CFirst test passes; second test fails due to count being 1
DBoth tests pass; count increments to 2
Attempts:
2 left
💡 Hint
Function scope means fixture runs fresh for each test function.
Predict Output
intermediate
2:00remaining
Effect of class scope on fixture invocation count
Given this pytest code, what is the value of count after running all tests?
PyTest
import pytest

count = 0

@pytest.fixture(scope='class')
def my_fixture():
    global count
    count += 1
    return count

class TestClass:
    def test_one(self, my_fixture):
        assert my_fixture == 1

    def test_two(self, my_fixture):
        assert my_fixture == 1
Acount is 1 after tests run
BTests fail due to fixture scope mismatch
Ccount is 2 after tests run
Dcount is 0 after tests run
Attempts:
2 left
💡 Hint
Class scope means fixture runs once per test class.
assertion
advanced
2:00remaining
Correct assertion for module scoped fixture reuse
Which assertion correctly verifies that a module scoped fixture is reused across multiple test functions?
PyTest
import pytest

@pytest.fixture(scope='module')
def resource():
    return []

def test_append_one(resource):
    resource.append(1)
    # What assertion here?

def test_append_two(resource):
    resource.append(2)
    # What assertion here?
Aassert resource == [1] in test_append_one and [1, 2] in test_append_two
Bassert resource == [1] in test_append_one and [2] in test_append_two
Cassert resource == [1] in test_append_one and resource == [1, 2] in test_append_two
Dassert resource == [1] in test_append_one and resource == [2] in test_append_two
Attempts:
2 left
💡 Hint
Module scope means the same fixture instance is shared across tests in the module.
🔧 Debug
advanced
2:00remaining
Debugging session scoped fixture with side effects
Why does this session scoped fixture cause tests to fail intermittently?
PyTest
import pytest

@pytest.fixture(scope='session')
def db_connection():
    conn = open_db_connection()
    yield conn
    conn.close()

def test_query_one(db_connection):
    assert db_connection.query('SELECT 1') == 1

def test_query_two(db_connection):
    assert db_connection.query('SELECT 2') == 2
AThe fixture is recreated for each test, causing conflicts.
BThe fixture closes the connection after the first test, so second test uses closed connection.
CThe fixture scope should be 'function' to avoid sharing.
DThe fixture does not yield the connection properly, causing NoneType errors.
Attempts:
2 left
💡 Hint
Session scope means fixture runs once per test session, but teardown happens after all tests.
🧠 Conceptual
expert
3:00remaining
Choosing fixture scope for expensive setup with multiple test classes
You have an expensive setup step needed for multiple test classes in a module. You want to run it only once per module but ensure each test class gets a fresh state derived from that setup. Which fixture scope strategy is best?
AUse a module scoped fixture for the expensive setup, then a class scoped fixture that depends on it to provide fresh state per class.
BUse a session scoped fixture for the setup and share the same state across all classes.
CUse function scoped fixtures only to ensure fresh state for every test.
DUse class scoped fixture for setup and module scoped fixture for state to optimize reuse.
Attempts:
2 left
💡 Hint
Think about separating expensive setup from per-class state initialization.