Verify deterministic behavior of a function that returns a fixed output
Preconditions (2)
✅ Expected Result: The test passes every time with the value 42 returned and assertion successful
Jump into concepts and practice - no test required
import pytest def get_fixed_value(): return 42 def test_get_fixed_value(): result = get_fixed_value() assert result == 42, f"Expected 42 but got {result}"
The get_fixed_value function always returns 42, making it deterministic.
The test test_get_fixed_value calls this function and asserts the result is 42.
This ensures the test will pass every time, showing deterministic behavior.
Assertions include a helpful message if the test fails.
Now add data-driven testing with 3 different fixed values returned by separate functions
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?