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.
Jump into concepts and practice - no test required
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.
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
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | pytest test runner initialized | — | PASS |
| 2 | Calls add_five(10) first time | Function add_five executes with input 10 | No assertion yet | PASS |
| 3 | Calls add_five(10) second time | Function add_five executes again with input 10 | No assertion yet | PASS |
| 4 | Assert result1 equals result2 | Both results are 15 | assert 15 == 15 | PASS |
| 5 | Assert result1 equals 15 | result1 is 15 | assert 15 == 15 | PASS |
| 6 | Test ends successfully | All assertions passed | — | PASS |
pytest?pytest test to make it deterministic?pytest test code, what will be the output when run twice?
import random
def test_random_number():
random.seed(42)
num = random.randint(1, 10)
assert num == 2pytest test sometimes fails randomly. What is the main issue?
import random
def test_random_fail():
num = random.randint(1, 10)
assert num == 5pytest test that checks if a function returns the current date. Which approach ensures determinism?