Deterministic tests always give the same result every time you run them. This helps you trust your tests and find real problems.
Deterministic tests in PyTest
Start learning this pattern below
Jump into concepts and practice - no test required
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.
def test_addition(): assert 2 + 3 == 5
def test_uppercase(): assert 'hello'.upper() == 'HELLO'
import random def test_random_fixed_seed(): random.seed(1) assert random.randint(1, 10) == 3
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.
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!')
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.
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.
Practice
pytest?Solution
Step 1: Understand the meaning of deterministic tests
Deterministic tests produce consistent results every time they run with the same inputs.Step 2: Compare options with this definition
Only The test always produces the same result given the same inputs. states that the test always produces the same result given the same inputs, matching the definition.Final Answer:
The test always produces the same result given the same inputs. -> Option BQuick Check:
Deterministic = Same result every time [OK]
- Confusing deterministic with fast tests
- Thinking randomness is allowed in deterministic tests
- Assuming external dependencies make tests deterministic
pytest test to make it deterministic?Solution
Step 1: Identify how to control randomness
Setting a fixed seed for the random number generator ensures the same random values each run.Step 2: Evaluate options for fixing randomness
Only Use a fixed seed for the random number generator. uses a fixed seed, which makes the test deterministic. Other options do not control randomness properly.Final Answer:
Use a fixed seed for the random number generator. -> Option AQuick Check:
Fixed seed = deterministic randomness [OK]
- Removing assertions does not fix randomness
- Averaging results does not make test deterministic
- Using uncontrolled random data causes flaky tests
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 == 2Solution
Step 1: Understand the effect of setting random.seed(42)
Setting the seed to 42 makes random.randint(1, 10) produce the same number every run.Step 2: Check the generated number and assertion
With seed 42, random.randint(1, 10) returns 2, so the assertion num == 2 passes every time.Final Answer:
The test will pass both times because the seed fixes the random number. -> Option AQuick Check:
Seed 42 -> randint = 2 == 2 -> assert passes [OK]
- Thinking the test passes because of seed (but checks wrong number)
- Assuming random changes every run despite seed
- Not verifying that seed(42) produces 2 for randint(1,10)
pytest test sometimes fails randomly. What is the main issue?
import random
def test_random_fail():
num = random.randint(1, 10)
assert num == 5Solution
Step 1: Identify randomness control in the test
The test uses random.randint without setting a seed, so output varies each run.Step 2: Understand why this causes test failure
Because the number is random, the assertion num == 5 sometimes fails, making the test flaky.Final Answer:
The test does not fix the random seed, causing non-deterministic results. -> Option CQuick Check:
No seed -> random output varies -> flaky test [OK]
- Thinking assertion syntax is wrong
- Assuming test name affects determinism
- Ignoring that random module is correctly imported
pytest test that checks if a function returns the current date. Which approach ensures determinism?Solution
Step 1: Understand why current date causes non-determinism
The current date changes every day, so tests using it without control will fail on different days.Step 2: Identify how to fix the date for deterministic testing
Mocking the current date to a fixed value ensures the test always sees the same date and passes consistently.Final Answer:
Mock the current date to a fixed value during the test. -> Option DQuick Check:
Mock date -> fixed input -> deterministic test [OK]
- Using real current date causes flaky tests
- Ignoring mocking leads to non-deterministic results
- Running test once does not guarantee determinism
