0
0
PyTesttesting~20 mins

Given-When-Then pattern in PyTest - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Given-When-Then Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this pytest Given-When-Then test?
Consider this pytest test using the Given-When-Then pattern. What will be the test result?
PyTest
import pytest

def add(a, b):
    return a + b

def test_addition():
    # Given two numbers
    x = 3
    y = 4

    # When they are added
    result = add(x, y)

    # Then the result should be 7
    assert result == 7
ATest passes successfully
BTest fails with AssertionError
CTest raises a NameError
DTest raises a TypeError
Attempts:
2 left
💡 Hint
Check the add function and the assertion carefully.
assertion
intermediate
1:30remaining
Which assertion correctly follows the Then step in Given-When-Then?
You want to check that a function returns a list with exactly 3 items. Which assertion fits the Then step best?
Aassert result is not None
Bassert len(result) == 3
Cassert result[0] == 3
Dassert type(result) == dict
Attempts:
2 left
💡 Hint
Focus on checking the number of items in the list.
🔧 Debug
advanced
2:30remaining
Why does this Given-When-Then test fail?
This pytest test using Given-When-Then fails. What is the cause?
PyTest
def multiply(a, b):
    return a * b

def test_multiply():
    # Given two numbers
    x = 2
    y = 5

    # When they are multiplied
    result = multiply(x, y)

    # Then the result should be 15
    assert result == 15
AThe assertion syntax is invalid
BThe multiply function has a syntax error
CThe variables x and y are not defined
DThe expected result in Then step is incorrect
Attempts:
2 left
💡 Hint
Calculate 2 times 5 and compare with the expected result.
🧠 Conceptual
advanced
1:30remaining
What is the main purpose of the Given-When-Then pattern in testing?
Choose the best description of the Given-When-Then pattern's purpose.
ATo write tests that run faster by skipping setup steps
BTo replace all assertions with print statements
CTo structure tests clearly by separating setup, action, and verification
DTo avoid writing any code in the test functions
Attempts:
2 left
💡 Hint
Think about how tests are organized for clarity.
framework
expert
3:00remaining
Which pytest fixture usage best fits the Given step in Given-When-Then?
You want to prepare test data before running the test action. Which fixture usage fits the Given step best?
A
@pytest.fixture
def setup_data():
    return {'user': 'alice', 'age': 30}

# In test:
# def test_user(setup_data):
#     ...
B
@pytest.mark.parametrize('input', [1, 2, 3])
def test_numbers(input):
    ...
C
def test_action():
    result = do_something()
    assert result is True
D
@pytest.fixture
def teardown_data():
    print('Cleaning up')
Attempts:
2 left
💡 Hint
Given step prepares data or state before the action.