0
0
PyTesttesting~10 mins

Autouse fixtures 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 make the fixture run automatically for each test.

PyTest
import pytest

@pytest.fixture([1])
def setup_data():
    print("Setup data")


def test_example():
    assert True
Drag options to blanks, or click blank then click option'
Ascope='module'
Bautouse=True
Cparams=[1,2]
Dname='setup'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'scope' instead of 'autouse' to run fixture automatically.
Forgetting to set autouse=True so fixture does not run automatically.
2fill in blank
medium

Complete the code to verify that the autouse fixture runs before the test function.

PyTest
import pytest

@pytest.fixture([1])
def setup_env():
    print("Environment setup")


def test_run():
    assert True
Drag options to blanks, or click blank then click option'
Aautouse=True
Bscope='session'
Cparams=[1]
Dname='env'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'scope' without autouse does not run fixture automatically.
Not adding any parameter so fixture is not triggered.
3fill in blank
hard

Fix the error in the fixture declaration to make it autouse.

PyTest
import pytest

@pytest.fixture(autouse=[1])
def prepare():
    print("Preparing")


def test_check():
    assert True
Drag options to blanks, or click blank then click option'
A"True"
B1
CTrue
DFalse
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'True' as a string instead of boolean True.
Using 1 instead of True which may cause confusion.
4fill in blank
hard

Fill both blanks to create an autouse fixture with module scope.

PyTest
import pytest

@pytest.fixture([1], [2])
def setup_module():
    print("Module setup")


def test_one():
    assert True
Drag options to blanks, or click blank then click option'
Aautouse=True
Bscope='function'
Cscope='module'
Dautouse=False
Attempts:
3 left
💡 Hint
Common Mistakes
Using scope='function' which runs fixture for each test function.
Setting autouse=False so fixture does not run automatically.
5fill in blank
hard

Fill all three blanks to create an autouse fixture with function scope and a print statement.

PyTest
import pytest

@pytest.fixture([1], [2])
def setup_func():
    print([3])


def test_sample():
    assert True
Drag options to blanks, or click blank then click option'
Aautouse=True
Bscope='function'
C"Function setup running"
D"Setup running"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong scope like 'module' when function scope is needed.
Not using autouse=True so fixture does not run automatically.
Incorrect print message string.