Verify sum of two numbers with assertion message
Preconditions (2)
✅ Expected Result: Test passes if sum is 5; if not, assertion fails with the message 'Sum of 2 and 3 should be 5'
import pytest def add_numbers(a: int, b: int) -> int: return a + b def test_add_numbers(): result = add_numbers(2, 3) assert result == 5, f"Sum of 2 and 3 should be 5 but got {result}"
This test defines a simple function add_numbers that returns the sum of two inputs.
The test function test_add_numbers calls this function with 2 and 3, then asserts the result equals 5.
The assertion includes a message that will show if the test fails, helping to quickly understand what went wrong.
This message uses an f-string to show the actual result, making debugging easier.
Now add data-driven testing with 3 different input pairs and expected sums
assert statement in pytest?assert condition, message.assert x == 5, "x should be 5" is correct.def test_value():
x = 3
assert x == 5, "x is not equal to 5"
def test_check():
y = 10
assert y > 5 "y should be greater than 5"
y > 5, causing a syntax error.def get_evens():
return [2, 4]
def test_evens():
result = get_evens()
# Choose the best assert with message
Pass the test and this pattern moves unknown