0
0
PyTesttesting~10 mins

Deterministic tests in PyTest - Test Execution Trace

Choose your learning style9 modes available
Test Overview

This test checks that a function returns the same output every time it is called with the same input. It verifies the test is deterministic, meaning it does not depend on random or changing data.

Test Code - pytest
PyTest
import pytest

def add_five(x):
    return x + 5

def test_add_five_deterministic():
    result1 = add_five(10)
    result2 = add_five(10)
    assert result1 == result2
    assert result1 == 15
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startspytest test runner initialized-PASS
2Calls add_five(10) first timeFunction add_five executes with input 10No assertion yetPASS
3Calls add_five(10) second timeFunction add_five executes again with input 10No assertion yetPASS
4Assert result1 equals result2Both results are 15assert 15 == 15PASS
5Assert result1 equals 15result1 is 15assert 15 == 15PASS
6Test ends successfullyAll assertions passed-PASS
Failure Scenario
Failing Condition: Function add_five returns different results for the same input
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the function add_five?
AIt modifies the input value
BIt returns random numbers
CIt always returns the same output for the same input
DIt raises an error for input 10
Key Result
Deterministic tests ensure functions produce consistent results for the same inputs, making tests reliable and easy to debug.