Bird
Raised Fist0
PyTesttesting~20 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of the Given-When-Then pattern in pytest tests?
easy
A. To write tests only for user interface elements
B. To organize tests into setup, action, and verification steps
C. To speed up test execution by skipping setup
D. To avoid using assertions in tests

Solution

  1. Step 1: Understand the pattern roles

    Given-When-Then divides a test into three parts: Given (setup), When (action), Then (check).
  2. Step 2: Match purpose with options

    Only To organize tests into setup, action, and verification steps correctly describes this organization purpose.
  3. Final Answer:

    To organize tests into setup, action, and verification steps -> Option B
  4. Quick Check:

    Given-When-Then = organize test steps [OK]
Hint: Remember: Given=setup, When=action, Then=check [OK]
Common Mistakes:
  • Thinking it only applies to UI tests
  • Believing it skips setup steps
  • Assuming no assertions are used
2. Which of the following is the correct way to write a Given-When-Then style pytest test function?
easy
A. def test_example(): # Given x = 5 # When y = x + 3 # Then assert y == 8
B. def test_example(): x = 5 y = x + 3 assert y == 8 # Given-When-Then comments missing
C. def test_example(): assert 5 + 3 == 8 # Given-When-Then not used
D. def test_example(): # When x = 5 # Given y = x + 3 # Then assert y == 8

Solution

  1. Step 1: Check comment order and code logic

    def test_example(): # Given x = 5 # When y = x + 3 # Then assert y == 8 correctly uses Given-When-Then comments in order with proper setup, action, and assertion.
  2. Step 2: Identify incorrect patterns

    The other options either lack Given-When-Then comments or have them in the wrong order (e.g., When before Given).
  3. Final Answer:

    def test_example():\n # Given\n x = 5\n # When\n y = x + 3\n # Then\n assert y == 8 -> Option A
  4. Quick Check:

    Correct comment order = def test_example(): # Given x = 5 # When y = x + 3 # Then assert y == 8 [OK]
Hint: Follow comment order: Given, When, Then [OK]
Common Mistakes:
  • Mixing the order of Given and When comments
  • Skipping comments entirely
  • Placing assertions outside Then step
3. Given the following pytest test using Given-When-Then pattern, what will be the test result?
def test_sum():
    # Given
    numbers = [1, 2, 3]
    # When
    total = sum(numbers)
    # Then
    assert total == 6
medium
A. Test will fail because assertion is wrong
B. Test will fail because sum is calculated incorrectly
C. Test will error due to missing import
D. Test will pass because sum of [1, 2, 3] is 6

Solution

  1. Step 1: Calculate sum of list

    The sum of [1, 2, 3] is 1+2+3 = 6.
  2. Step 2: Check assertion correctness

    The assertion checks total == 6, which is true, so test passes.
  3. Final Answer:

    Test will pass because sum of [1, 2, 3] is 6 -> Option D
  4. Quick Check:

    sum([1,2,3]) = 6 [OK]
Hint: Calculate sum and compare with assertion value [OK]
Common Mistakes:
  • Assuming sum returns a list instead of number
  • Thinking assertion expects a different value
  • Confusing test error with failure
4. Identify the error in this Given-When-Then pytest test:
def test_multiply():
    # Given
    x = 4
    y = 5
    # When
    result = x * y
    # Then
    assert result = 20
medium
A. Syntax error in assertion statement
B. Wrong variable names used
C. Missing setup in Given step
D. No assertion in Then step

Solution

  1. Step 1: Check assertion syntax

    The assertion uses single '=' which is assignment, not comparison. It should be '==' for comparison.
  2. Step 2: Confirm other parts are correct

    Variables and steps are correct; only assertion syntax is wrong.
  3. Final Answer:

    Syntax error in assertion statement -> Option A
  4. Quick Check:

    Use '==' in assert, not '=' [OK]
Hint: Use '==' for assert comparisons, not '=' [OK]
Common Mistakes:
  • Using '=' instead of '==' in assert
  • Confusing assignment with comparison
  • Ignoring syntax errors in assertions
5. You want to test a function that filters out falsy values (like 0, '', None) from a list using Given-When-Then pattern in pytest. Which test code correctly applies this pattern and checks the result?
hard
A. def test_filter_falsy(): # Given data = [0, 1, '', 'hello', None, True] # Then assert filtered == [1, 'hello', True] # When filtered = [x for x in data if x]
B. def test_filter_falsy(): data = [0, 1, '', 'hello', None, True] filtered = [x for x in data if x] assert filtered == [1, 'hello', True]
C. def test_filter_falsy(): # Given data = [0, 1, '', 'hello', None, True] # When filtered = [x for x in data if x] # Then assert filtered == [1, 'hello', True]
D. def test_filter_falsy(): # Given data = [0, 1, '', 'hello', None, True] # When filtered = filter(lambda x: x, data) # Then assert list(filtered) == [0, 1, '', 'hello', None, True]

Solution

  1. Step 1: Verify Given-When-Then structure

    def test_filter_falsy(): # Given data = [0, 1, '', 'hello', None, True] # When filtered = [x for x in data if x] # Then assert filtered == [1, 'hello', True] correctly uses Given for data setup, When for filtering action, Then for assertion check.
  2. Step 2: Check correctness of filtering and assertion

    Filtering removes falsy values; expected list matches filtered result. Other options miss comments or have wrong assertion.
  3. Final Answer:

    def test_filter_falsy():\n # Given\n data = [0, 1, '', 'hello', None, True]\n # When\n filtered = [x for x in data if x]\n # Then\n assert filtered == [1, 'hello', True] -> Option C
  4. Quick Check:

    Given-When-Then + correct filter = def test_filter_falsy(): # Given data = [0, 1, '', 'hello', None, True] # When filtered = [x for x in data if x] # Then assert filtered == [1, 'hello', True] [OK]
Hint: Keep steps clear: setup, action, then check [OK]
Common Mistakes:
  • Skipping Given-When-Then comments
  • Asserting wrong filtered list
  • Using filter object without converting to list