Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a fixture using pytest.
PyTest
import pytest @pytest.[1]() def sample_data(): return [1, 2, 3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using @pytest.test instead of @pytest.fixture
Forgetting the parentheses after the decorator
Confusing fixture with parametrize
✗ Incorrect
The @pytest.fixture decorator is used to define a fixture function that can be used in tests.
2fill in blank
mediumComplete the test function to use the fixture named 'sample_data'.
PyTest
def test_sum([1]): assert sum(sample_data) == 6
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture
Not including the fixture as a parameter
Trying to call the fixture inside the test instead of using it as a parameter
✗ Incorrect
To use a fixture in a test, include its name as a parameter in the test function.
3fill in blank
hardFix the error in the fixture definition by completing the decorator correctly.
PyTest
import pytest @[1] def config(): return {'url': 'http://example.com'}
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses in the decorator
Using '@pytest.test' instead of '@pytest.fixture()'
Using '@pytest.setup' which is not a valid decorator
✗ Incorrect
The @pytest.fixture decorator must include parentheses even if no arguments are passed.
4fill in blank
hardFill both blanks to create a fixture that runs once per module and a test that uses it.
PyTest
import pytest @pytest.fixture(scope=[1]) def resource(): return 'db_connection' def test_resource([2]): assert resource == 'db_connection'
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using scope 'function' which runs before every test
Using a different parameter name than the fixture
Forgetting quotes around the scope value
✗ Incorrect
The fixture scope 'module' makes it run once per module. The test uses the fixture by naming it as a parameter.
5fill in blank
hardFill all three blanks to create a fixture with autouse enabled and a test that uses it implicitly.
PyTest
import pytest @pytest.fixture(autouse=[1]) def setup_env(): print('Setup environment') def test_example(): assert True # setup_env is used [2] without being [3] as a parameter
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting autouse to False so fixture does not run automatically
Thinking the fixture must be passed as a parameter when autouse=True
Misunderstanding implicit usage of fixtures
✗ Incorrect
Setting autouse=True makes the fixture run automatically. The test uses it implicitly without passing it as a parameter.