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.
Jump into concepts and practice - no test required
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.
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}"
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Arrange inputs: num1=3, num2=5, expected_sum=8 | Variables set in test function | — | PASS |
| 3 | Act by calling add(num1, num2) | Function add called with arguments 3 and 5 | — | PASS |
| 4 | Assert that result equals expected_sum | Result is 8, expected_sum is 8 | assert 8 == 8 | PASS |
| 5 | Test ends | Test passed with no errors | — | PASS |
def test_sum():
# Arrange
numbers = [1, 2, 3]
expected = 6
# Act
result = sum(numbers)
# Assert
assert result == expected
def test_uppercase():
# Arrange
text = "hello"
# Act
result = text.upper
# Assert
assert result == "HELLO"
divide(a, b) that returns the division of two numbers. Using Arrange-Act-Assert, which test correctly checks that dividing by zero raises a ZeroDivisionError?with pytest.raises(ZeroDivisionError): correctly checks for the exception during Act step.