Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a fixture named db.
PyTest
import pytest @pytest.fixture def [1](): return "database connection"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixture name that does not match the resource purpose.
Forgetting the @pytest.fixture decorator.
✗ Incorrect
The fixture is named 'db' to represent a database connection resource.
2fill in blank
mediumComplete the code to make the user fixture depend on the db fixture.
PyTest
import pytest @pytest.fixture def db(): return "database connection" @pytest.fixture def user([1]): return f"user using {db}"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong parameter name that does not match any fixture.
Not including the dependency fixture as a parameter.
✗ Incorrect
The 'user' fixture depends on the 'db' fixture, so 'db' must be a parameter.
3fill in blank
hardFix the error in the test function to use the user fixture correctly.
PyTest
def test_user_access([1]): assert user == "user using database connection"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a fixture name not defined or not passed as parameter.
Trying to access fixture variables without declaring them as parameters.
✗ Incorrect
The test function must accept the 'user' fixture as a parameter to use it.
4fill in blank
hardFill both blanks to create a fixture session that depends on db and returns a session string.
PyTest
@pytest.fixture def session([1]): return f"session with [2]"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the parameter and the string interpolation.
Forgetting to include the dependency fixture as a parameter.
✗ Incorrect
The fixture 'session' depends on 'db' and returns a string using 'db'.
5fill in blank
hardFill all three blanks to define a test that uses the session fixture and asserts its value.
PyTest
def test_session_value([1]): expected = "session with [2]" assert [3] == expected
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong fixture names in parameters or assertions.
Mismatching expected string and actual fixture value.
✗ Incorrect
The test uses the 'session' fixture, expects a string with 'database connection', and asserts the fixture value.