0
0
PyTesttesting~5 mins

Deterministic tests in PyTest - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does it mean for a test to be deterministic?
A deterministic test always produces the same result when run with the same code and inputs, no matter how many times it runs.
Click to reveal answer
beginner
Why are deterministic tests important in software testing?
They help ensure reliability and trust in test results because failures are consistent and reproducible, making debugging easier.
Click to reveal answer
intermediate
How can randomness affect test determinism?
Randomness can cause tests to behave differently each run, leading to flaky tests that sometimes pass and sometimes fail.
Click to reveal answer
intermediate
What is a common way to make tests deterministic when they use random values?
Set a fixed seed for the random number generator so it produces the same sequence of values every time the test runs.
Click to reveal answer
beginner
Example: In pytest, how do you fix randomness to make a test deterministic?
Use code like <code>import random
random.seed(42)</code> at the start of your test to fix the random seed.
Click to reveal answer
What is a key characteristic of a deterministic test?
AIt sometimes passes and sometimes fails unpredictably.
BIt uses random values to test different cases.
CIt always produces the same result given the same inputs.
DIt runs only once and never again.
Which practice helps make tests deterministic when randomness is involved?
AIgnoring random values in tests.
BUsing different random seeds each run.
CRunning tests multiple times to average results.
DSetting a fixed seed for the random number generator.
What problem do non-deterministic (flaky) tests cause?
AThey make debugging harder because failures are inconsistent.
BThey always pass, hiding bugs.
CThey run faster than deterministic tests.
DThey require no maintenance.
In pytest, where should you set the random seed to ensure determinism?
AOnly in the main application code.
BInside the test function before using random values.
CAfter the test finishes.
DYou cannot set a random seed in pytest.
Which of these is NOT a cause of non-deterministic tests?
AUsing fixed inputs and no randomness.
BRelying on external systems with changing data.
CUsing random values without fixed seeds.
DTests depending on time or date.
Explain what deterministic tests are and why they matter in software testing.
Think about how tests behave when run multiple times with the same code.
You got /3 concepts.
    Describe how you would make a pytest test deterministic if it uses random numbers.
    Consider the random.seed() function and its placement.
    You got /3 concepts.