0
0
PyTesttesting~10 mins

Arrange-Act-Assert pattern in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test uses the Arrange-Act-Assert pattern to check if the add function correctly adds two numbers. It arranges the inputs, acts by calling the function, and asserts the expected result.

Test Code - pytest
PyTest
import pytest

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

def test_add_two_numbers():
    # Arrange
    num1 = 3
    num2 = 5
    expected_sum = 8

    # Act
    result = add(num1, num2)

    # Assert
    assert result == expected_sum, f"Expected {expected_sum} but got {result}"
Execution Trace - 5 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Arrange inputs: num1=3, num2=5, expected_sum=8Variables set in test function-PASS
3Act by calling add(num1, num2)Function add called with arguments 3 and 5-PASS
4Assert that result equals expected_sumResult is 8, expected_sum is 8assert 8 == 8PASS
5Test endsTest passed with no errors-PASS
Failure Scenario
Failing Condition: The add function returns a wrong sum, e.g., 7 instead of 8
Execution Trace Quiz - 3 Questions
Test your understanding
What is the purpose of the 'Arrange' step in this test?
ACall the function being tested
BSet up the inputs and expected results
CCheck if the test passed
DClean up after the test
Key Result
Using the Arrange-Act-Assert pattern helps keep tests clear and easy to understand by separating setup, action, and verification steps.