0
0
PyTesttesting~10 mins

Test functions in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks if the function add_numbers correctly adds two numbers and returns the expected result.

Test Code - PyTest
PyTest
import pytest

def add_numbers(a, b):
    return a + b

def test_add_numbers():
    result = add_numbers(3, 4)
    assert result == 7, f"Expected 7 but got {result}"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startsPyTest test runner initialized-PASS
2PyTest discovers test function 'test_add_numbers'Test function ready to run-PASS
3Calls function add_numbers(3, 4)Function executes and returns 7-PASS
4Assert that result == 7Result is 7Check if 7 equals 7PASS
5Test completes successfullyTest passed with no errors-PASS
Failure Scenario
Failing Condition: The add_numbers function returns a wrong sum, e.g., 8 instead of 7
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test function 'test_add_numbers' verify?
AThat add_numbers(3, 4) returns 7
BThat add_numbers(3, 4) returns 8
CThat add_numbers raises an error
DThat add_numbers returns a string
Key Result
Always write simple test functions that clearly check one behavior using assert statements for easy debugging.