Verify addition of two numbers using Given-When-Then pattern
Preconditions (2)
✅ Expected Result: The test passes confirming the sum is correct
Jump into concepts and practice - no test required
import pytest # Test function following Given-When-Then pattern def test_add_two_numbers(): # Given two numbers num1 = 3 num2 = 5 # When they are added result = num1 + num2 # Then the result should be 8 assert result == 8, f"Expected sum to be 8 but got {result}"
This test uses the Given-When-Then pattern to organize the steps clearly.
Given: We set two numbers num1 and num2.
When: We perform the addition operation.
Then: We assert the result equals 8 using assert. This makes the test easy to read and understand.
Now add data-driven testing with 3 different pairs of numbers and their expected sums
def test_sum():
# Given
numbers = [1, 2, 3]
# When
total = sum(numbers)
# Then
assert total == 6def test_multiply():
# Given
x = 4
y = 5
# When
result = x * y
# Then
assert result = 20