0
0
PyTesttesting~10 mins

Assert with messages in PyTest - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - pytest
PyTest
import pytest

def test_sum():
    a = 5
    b = 7
    result = a + b
    assert result == 12, f"Expected sum to be 12 but got {result}"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Assign values a=5 and b=7Variables a and b set in memory-PASS
3Calculate result = a + bresult variable holds value 12-PASS
4Assert that result == 12 with messageCheck if result equals 12Assert result == 12PASS
5Test ends successfullyTest passed with no errors-PASS
Failure Scenario
Failing Condition: If the sum result is not 12
Execution Trace Quiz - 3 Questions
Test your understanding
What does the assertion message do in this test?
AIt changes the sum calculation
BIt explains why the test failed if the sum is incorrect
CIt skips the test if the sum is wrong
DIt prints the sum regardless of the test result
Key Result
Always add clear messages to assertions to help quickly understand why a test failed.