Test Overview
This test checks if the sum of two numbers is correct. It uses an assertion with a message to explain the failure if the sum is wrong.
This test checks if the sum of two numbers is correct. It uses an assertion with a message to explain the failure if the sum is wrong.
import pytest def test_sum(): a = 5 b = 7 result = a + b assert result == 12, f"Expected sum to be 12 but got {result}"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Assign values a=5 and b=7 | Variables a and b set in memory | — | PASS |
| 3 | Calculate result = a + b | result variable holds value 12 | — | PASS |
| 4 | Assert that result == 12 with message | Check if result equals 12 | Assert result == 12 | PASS |
| 5 | Test ends successfully | Test passed with no errors | — | PASS |
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