Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a fixture that returns a simple string.
PyTest
import pytest @pytest.fixture def greeting(): return [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Returning a variable name instead of a string literal.
✗ Incorrect
The fixture should return a string literal, so it must be quoted. Option A correctly returns the string "hello".
2fill in blank
mediumComplete the code to use the 'greeting' fixture inside another fixture.
PyTest
import pytest @pytest.fixture def greeting(): return "hello" @pytest.fixture def welcome_message([1]): return f"{greeting}, world!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture name.
Not passing the fixture as a parameter.
✗ Incorrect
To use one fixture inside another, you add it as a parameter with the same name. Here, 'greeting' is the fixture to use.
3fill in blank
hardFix the error in the test function to use the 'welcome_message' fixture correctly.
PyTest
def test_message([1]): assert welcome_message == "hello, world!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture name.
Not accepting the fixture as a parameter.
✗ Incorrect
The test function must accept the fixture name as a parameter to use its value. Here, 'welcome_message' is the fixture to use.
4fill in blank
hardFill both blanks to create a fixture that modifies the 'greeting' fixture by adding punctuation.
PyTest
@pytest.fixture def excited_greeting([1]): return [2] + "!"
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the fixture parameter as a function.
Using a wrong parameter name.
✗ Incorrect
The fixture 'excited_greeting' takes 'greeting' as a parameter (the fixture itself), and returns it with an exclamation mark. Using 'greeting' (the parameter) is correct; calling it as a function is incorrect.
5fill in blank
hardFill all three blanks to write a test that uses the 'excited_greeting' fixture and asserts its value.
PyTest
def test_excited([1]): assert [2] == [3]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong fixture name as parameter.
Comparing to the wrong expected string.
Not accepting the fixture as a parameter.
✗ Incorrect
The test function accepts the 'excited_greeting' fixture as a parameter, asserts that its value equals the string "hello!".