0
0
PyTesttesting~10 mins

Fixture composition 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 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'
A"hello"
Bhello
C'greet'
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to put quotes around the string.
Returning a variable name instead of a string literal.
2fill in blank
medium

Complete 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'
Amsg
Bwelcome
Cmessage
Dgreeting
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture name.
Not passing the fixture as a parameter.
3fill in blank
hard

Fix 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'
Agreeting
Bmessage
Cwelcome_message
Dmsg
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different parameter name than the fixture name.
Not accepting the fixture as a parameter.
4fill in blank
hard

Fill 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'
Agreeting
Bgreeting()
Dexcited
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the fixture parameter as a function.
Using a wrong parameter name.
5fill in blank
hard

Fill 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'
Aexcited_greeting
C"hello!"
Dgreeting
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.