0
0
PyTesttesting~10 mins

Fixture scope (function, class, module, session) in PyTest - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a fixture with function scope.

PyTest
import pytest

@pytest.fixture(scope=[1])
def sample_fixture():
    return 42
Drag options to blanks, or click blank then click option'
Afunction
Bclass
Cmodule
Dsession
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing 'class' or 'module' which run less frequently than function scope.
Using 'session' which runs once per test session.
2fill in blank
medium

Complete the code to set a fixture with class scope.

PyTest
import pytest

@pytest.fixture(scope=[1])
def class_fixture():
    return 'class level resource'
Drag options to blanks, or click blank then click option'
Amodule
Bsession
Cclass
Dfunction
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing class scope with function or module scope.
Using 'session' which is broader than class scope.
3fill in blank
hard

Fix the error in the fixture scope to make it run once per module.

PyTest
import pytest

@pytest.fixture(scope=[1])
def module_fixture():
    return 'module resource'
Drag options to blanks, or click blank then click option'
Afunction
Bclass
Csession
Dmodule
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' scope which runs once per class, not per module.
Using 'session' which runs once per entire test session.
4fill in blank
hard

Fill both blanks to create a session-scoped fixture and use it in a test.

PyTest
import pytest

@pytest.fixture(scope=[1])
def session_fixture():
    return 'session resource'

def test_example([2]):
    assert session_fixture == 'session resource'
Drag options to blanks, or click blank then click option'
Asession
Bfunction
Csession_fixture
Dmodule_fixture
Attempts:
3 left
💡 Hint
Common Mistakes
Not matching the fixture name in the test function parameter.
Using wrong scope like 'function' for session-wide resource.
5fill in blank
hard

Fill all three blanks to create a module-scoped fixture, use it in a test, and assert its value.

PyTest
import pytest

@pytest.fixture(scope=[1])
def mod_fixture():
    return 100

def test_mod([2]):
    assert [3] == 100
Drag options to blanks, or click blank then click option'
Amodule
Bmod_fixture
Dfunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong scope like 'function' instead of 'module'.
Mismatching fixture name in test function parameter or assertion.