Challenge - 5 Problems
Given-When-Then Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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
Attempts:
2 left
💡 Hint
Check the add function and the assertion carefully.
✗ Incorrect
The add function correctly sums 3 and 4 to 7. The assertion checks if result == 7, which is true, so the test passes.
❓ assertion
intermediate1: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?
Attempts:
2 left
💡 Hint
Focus on checking the number of items in the list.
✗ Incorrect
The Then step verifies the expected outcome. Checking length equals 3 confirms the list size is correct.
🔧 Debug
advanced2: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
Attempts:
2 left
💡 Hint
Calculate 2 times 5 and compare with the expected result.
✗ Incorrect
2 multiplied by 5 equals 10, but the test expects 15, so the assertion fails.
🧠 Conceptual
advanced1: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.
Attempts:
2 left
💡 Hint
Think about how tests are organized for clarity.
✗ Incorrect
Given-When-Then helps organize tests into setup (Given), action (When), and check (Then) for readability and maintainability.
❓ framework
expert3: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?
Attempts:
2 left
💡 Hint
Given step prepares data or state before the action.
✗ Incorrect
Fixture setup_data prepares test data before the test runs, matching the Given step role.