Recall & Review
beginner
What is a fixture dependency in pytest?
A fixture dependency happens when one fixture uses another fixture to set up its environment. This means one fixture can call another fixture to reuse setup code.
Click to reveal answer
beginner
How do you make a fixture use another fixture in pytest?
You add the other fixture as a parameter to your fixture function. Pytest will automatically run the dependent fixture first and pass its result.
Click to reveal answer
beginner
Example: What does this code do?
@pytest.fixture
def db():
return "database connection"
@pytest.fixture
def user(db):
return f"user with {db}"The 'user' fixture depends on the 'db' fixture. When 'user' runs, pytest first runs 'db' and passes its result. So 'user' returns 'user with database connection'.
Click to reveal answer
beginner
Why use fixture dependencies?
Fixture dependencies help avoid repeating setup code. They make tests cleaner and easier to maintain by reusing common setup steps.
Click to reveal answer
intermediate
What happens if a fixture dependency fails?
If a fixture that another fixture depends on fails, pytest will stop and mark the test as failed or error because the setup is incomplete.
Click to reveal answer
How do you declare that one fixture depends on another in pytest?
✗ Incorrect
In pytest, you declare fixture dependencies by listing the other fixture as a parameter in your fixture function.
What will pytest do if a fixture dependency fails during setup?
✗ Incorrect
If a fixture dependency fails, pytest stops the test and marks it as failed or error because the setup is incomplete.
Why is using fixture dependencies beneficial?
✗ Incorrect
Fixture dependencies help reuse setup code, making tests cleaner and easier to maintain.
In this code, what is the output of the 'user' fixture?
@pytest.fixture
def db():
return "db connection"
@pytest.fixture
def user(db):
return f"user with {db}"✗ Incorrect
The 'user' fixture uses the 'db' fixture and returns 'user with db connection'.
How does pytest know the order to run fixtures with dependencies?
✗ Incorrect
Pytest runs fixtures that are dependencies first, then passes their results to the fixtures that depend on them.
Explain how fixture dependencies work in pytest and why they are useful.
Think about how one setup step can build on another.
You got /4 concepts.
Describe what happens when a fixture that another fixture depends on fails during test setup.
Consider the chain reaction of failures in setup.
You got /4 concepts.