Can you define a fixture that depends on another fixture? How does this establish setup order?

Python interview question for Advanced practice.

Answer

Yes, fixtures can request other fixtures as arguments. Order: Pytest builds a dependency graph. If user requests db, Pytest ensures db is fully set up (its yield hasn't finished) before user starts setup. Conversely, user is torn down before db is torn down. This allows building complex, layered environments (App - DB - Table - Row).

Explanation

Pytest resolves the dependency graph topologically.

Related Questions