Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What does test independence mean in software testing?
Test independence means each test runs on its own without relying on other tests. This helps find bugs clearly and makes tests easier to fix.
Click to reveal answer
beginner
Why should tests not share state or data?
Sharing state can cause tests to pass or fail depending on the order they run. Independent tests avoid this problem by keeping data separate.
Click to reveal answer
intermediate
How does pytest help keep tests independent?
Pytest runs each test function separately and can use fixtures to set up fresh data for each test, ensuring no leftover data affects others.
Click to reveal answer
beginner
What is a common problem if tests are not independent?
Tests might pass or fail randomly depending on the order they run, making it hard to trust test results and fix bugs.
Click to reveal answer
intermediate
Show a simple pytest example that ensures test independence using fixtures.
import pytest
@pytest.fixture
def fresh_list():
return []
def test_add_item(fresh_list):
fresh_list.append(1)
assert fresh_list == [1]
def test_empty_list(fresh_list):
assert fresh_list == []
Each test gets a new empty list, so they don't affect each other.
Click to reveal answer
What is the main benefit of test independence?
ATests share data to save memory
BTests run faster together
CTests run without affecting each other
DTests depend on previous tests
✗ Incorrect
Independent tests run without relying on others, so results are clear and reliable.
In pytest, what feature helps create fresh data for each test?
AFixtures
BClasses
CDecorators
DAssertions
✗ Incorrect
Fixtures set up fresh data or state for each test, helping keep tests independent.
What can happen if tests share state?
ATests may pass or fail unpredictably
BTests always run faster
CTests become shorter
DTests never fail
✗ Incorrect
Sharing state can cause tests to depend on order, making results unreliable.
Which is a good practice to keep tests independent?
ASkip tests randomly
BRun tests in a fixed order
CShare global variables
DReset data before each test
✗ Incorrect
Resetting data ensures each test starts fresh and does not depend on others.
If test A fails only when test B runs before it, what is the problem?
ATests have too many assertions
BTests are not independent
CTests are too fast
DTests use fixtures
✗ Incorrect
Test A depends on test B's state, breaking independence.
Explain why test independence is important and how pytest fixtures help achieve it.
Think about tests running alone and not affecting each other.
You got /4 concepts.
Describe a simple pytest example that shows two independent tests using fixtures.
Remember the example with a fresh list for each test.
You got /4 concepts.
Practice
(1/5)
1. Why is test independence important in pytest?
easy
A. It groups tests to run in a specific order.
B. It allows tests to share variables for faster execution.
C. It ensures tests do not affect each other and run reliably alone.
D. It makes tests run only when previous tests pass.
Solution
Step 1: Understand test independence concept
Test independence means each test runs alone without relying on others.
Step 2: Identify why independence matters
This prevents tests from failing due to side effects or order, making results reliable.
Final Answer:
It ensures tests do not affect each other and run reliably alone. -> Option C
Quick Check:
Test independence = tests run alone [OK]
Hint: Tests should run alone without relying on others [OK]
Common Mistakes:
Thinking tests must share data to be efficient
Believing tests run only if previous tests pass
Assuming test order controls correctness
2. Which pytest feature helps keep tests independent by running setup code before each test?
easy
A. Using setup_method or setup_function
B. Using yield_fixture to share data
C. Using @pytest.mark.parametrize
D. Using pytest.skip() to skip tests
Solution
Step 1: Identify setup features in pytest
Pytest runs setup code before each test using setup_method or setup_function.
Step 2: Understand their role in test independence
Setup prepares fresh state for each test, avoiding shared state and keeping tests independent.
Final Answer:
Using setup_method or setup_function -> Option A
Quick Check:
Setup before each test = setup_method/setup_function [OK]
Hint: Setup code before each test keeps tests independent [OK]
Common Mistakes:
Confusing parameterize with setup
Using yield_fixture to share state incorrectly
Skipping tests does not setup state
3. Given the code below, what will be the output when running both tests?