0
0
PyTesttesting~10 mins

Given-When-Then pattern 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 mark the 'Given' step in a pytest test function.

PyTest
def test_addition():
    # [1]: Setup numbers
    a = 2
    b = 3
    result = a + b
    assert result == 5
Drag options to blanks, or click blank then click option'
AAssert
BWhen
CThen
DGiven
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'Given' with 'When' or 'Then' steps.
2fill in blank
medium

Complete the code to mark the 'When' step where the action happens.

PyTest
def test_subtraction():
    a = 5
    b = 3
    # [1]: Perform subtraction
    result = a - b
    assert result == 2
Drag options to blanks, or click blank then click option'
AWhen
BSetup
CThen
DGiven
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Given' or 'Then' instead of 'When' for the action step.
3fill in blank
hard

Fix the error in the 'Then' step comment describing the expected outcome.

PyTest
def test_multiplication():
    a = 4
    b = 3
    result = a * b
    # [1]: Check if result equals 12
    assert result == 12
Drag options to blanks, or click blank then click option'
AGiven
BWhen
CThen
DCheck
Attempts:
3 left
💡 Hint
Common Mistakes
Labeling the 'Then' step as 'When' or 'Given'.
4fill in blank
hard

Fill both blanks to complete the Given-When-Then comments in the test.

PyTest
def test_division():
    # [1]: Setup numbers
    numerator = 10
    denominator = 2
    # [2]: Perform division
    result = numerator / denominator
    assert result == 5
Drag options to blanks, or click blank then click option'
AGiven
BWhen
CThen
DSetup
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping 'Given' and 'When' labels.
5fill in blank
hard

Fill all three blanks to complete the Given-When-Then pattern in the test function.

PyTest
def test_string_upper():
    # [1]: Setup input string
    input_str = "hello"
    # [2]: Convert string to uppercase
    result = input_str.upper()
    # [3]: Verify the result is uppercase
    assert result == "HELLO"
Drag options to blanks, or click blank then click option'
AGiven
BWhen
CThen
DAssert
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up the order of Given, When, Then.