What if your tests stopped playing tricks on you and always told the truth?
Why Deterministic tests in PyTest? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine running your tests manually every time you change your code. Sometimes they pass, sometimes they fail, and you can't tell why. It's like flipping a coin and hoping for heads.
Manual testing is slow and tiring. You might miss bugs because results change randomly. It's hard to trust tests that don't behave the same way every time.
Deterministic tests always give the same result for the same input. They remove guesswork and make debugging easier. You can trust your tests to catch real problems.
import random def test_random_behavior(): assert random.choice([True, False]) == True
import random def test_fixed_behavior(): random.seed(42) assert random.choice([True, False]) == True
Deterministic tests let you confidently fix bugs and improve code without fear of random failures.
When testing a game, deterministic tests ensure the same moves always lead to the same results, so you can find and fix errors reliably.
Manual tests can be unpredictable and unreliable.
Deterministic tests produce consistent, repeatable results.
This builds trust and speeds up finding real bugs.
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
