Want to write tests that tell a clear story anyone can follow?
Why Given-When-Then pattern in PyTest? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine testing a calculator app by clicking buttons and writing down results on paper every time you want to check if addition works.
This manual way is slow and easy to mess up. You might forget steps or mix results, making it hard to trust your testing.
The Given-When-Then pattern helps organize tests clearly: Given some setup, When an action happens, Then expect a result. It makes tests easy to read and follow.
def test_add(): result = add(2, 3) assert result == 5
def test_add(): # Given two numbers a, b = 2, 3 # When they are added result = add(a, b) # Then the result should be 5 assert result == 5
This pattern makes tests clear and simple, so anyone can understand what is tested and why.
When testing a login feature, you can say: Given a user with valid credentials, When they enter them and press login, Then they should see the homepage.
Manual testing is slow and error-prone.
Given-When-Then organizes tests into clear steps.
It improves test readability and trust.
Practice
Solution
Step 1: Understand the pattern roles
Given-When-Then divides a test into three parts: Given (setup), When (action), Then (check).Step 2: Match purpose with options
Only To organize tests into setup, action, and verification steps correctly describes this organization purpose.Final Answer:
To organize tests into setup, action, and verification steps -> Option BQuick Check:
Given-When-Then = organize test steps [OK]
- Thinking it only applies to UI tests
- Believing it skips setup steps
- Assuming no assertions are used
Solution
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.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).Final Answer:
def test_example():\n # Given\n x = 5\n # When\n y = x + 3\n # Then\n assert y == 8 -> Option AQuick Check:
Correct comment order = def test_example(): # Given x = 5 # When y = x + 3 # Then assert y == 8 [OK]
- Mixing the order of Given and When comments
- Skipping comments entirely
- Placing assertions outside Then step
def test_sum():
# Given
numbers = [1, 2, 3]
# When
total = sum(numbers)
# Then
assert total == 6Solution
Step 1: Calculate sum of list
The sum of [1, 2, 3] is 1+2+3 = 6.Step 2: Check assertion correctness
The assertion checks total == 6, which is true, so test passes.Final Answer:
Test will pass because sum of [1, 2, 3] is 6 -> Option DQuick Check:
sum([1,2,3]) = 6 [OK]
- Assuming sum returns a list instead of number
- Thinking assertion expects a different value
- Confusing test error with failure
def test_multiply():
# Given
x = 4
y = 5
# When
result = x * y
# Then
assert result = 20Solution
Step 1: Check assertion syntax
The assertion uses single '=' which is assignment, not comparison. It should be '==' for comparison.Step 2: Confirm other parts are correct
Variables and steps are correct; only assertion syntax is wrong.Final Answer:
Syntax error in assertion statement -> Option AQuick Check:
Use '==' in assert, not '=' [OK]
- Using '=' instead of '==' in assert
- Confusing assignment with comparison
- Ignoring syntax errors in assertions
Solution
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.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.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 CQuick 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]
- Skipping Given-When-Then comments
- Asserting wrong filtered list
- Using filter object without converting to list
