Deterministic tests always give the same result every time you run them. This helps you trust your tests and find real problems.
0
0
Deterministic tests in PyTest
Introduction
When you want to check if a function always returns the same output for the same input.
When you need to avoid random or unpredictable behavior in your tests.
When you want to make debugging easier by having repeatable test results.
When testing code that depends on time or random numbers, and you want consistent results.
When running tests in a team to ensure everyone sees the same test outcomes.
Syntax
PyTest
def test_function(): result = function_to_test(input) assert result == expected_output
Use fixed inputs and expected outputs to keep tests deterministic.
Avoid using random values or current time directly in tests without control.
Examples
This test always passes because 2 + 3 is always 5.
PyTest
def test_addition(): assert 2 + 3 == 5
This test checks a string method that always returns the same result.
PyTest
def test_uppercase(): assert 'hello'.upper() == 'HELLO'
Setting a fixed seed makes random numbers predictable and test deterministic.
PyTest
import random def test_random_fixed_seed(): random.seed(1) assert random.randint(1, 10) == 3
Sample Program
This test fixes the random seed so the random number is always the same. The test checks the function output with that fixed random number.
PyTest
import random def add_random_number(x): return x + random.randint(1, 10) def test_add_random_number(): random.seed(42) # Fix seed for determinism result = add_random_number(5) assert result == 5 + 2 # random.randint(1,10) with seed 42 returns 2 if __name__ == '__main__': test_add_random_number() print('Test passed!')
OutputSuccess
Important Notes
Always control sources of randomness or time in your tests to keep them deterministic.
Deterministic tests make it easier to find bugs because failures are consistent.
Use pytest fixtures or mocks to replace unpredictable parts of your code during testing.
Summary
Deterministic tests always produce the same result for the same inputs.
Fix random seeds or mock time to avoid unpredictable behavior.
Consistent tests help you trust your code and debug faster.