KatInterview

pytest Fixtures, Decorators, and Generators: Core Python Mechanics

System DesignModel answer
EasyModel answerSystem Designpythonpytestgenerators

Last updated

Three-part fundamentals question on how pytest fixtures are scoped and torn down, how decorators wrap functions, and how generators stream data lazily.

Answer all three parts below. Where a short code snippet makes your explanation more precise, include one — working examples are preferred over abstract descriptions.


Part 1 — pytest Fixtures

Explain what a pytest fixture is and how a test function receives one. Cover the following:

  • How pytest discovers a fixture and passes its return value into a test, including the case where one fixture depends on another
  • The four available scopes (function, class, module, session), the speed-versus-isolation trade-off each one represents, and the rule that governs mixing scopes of different lifetimes
  • How a fixture performs teardown, what mechanism controls whether teardown runs when a test fails, and when teardown is skipped entirely
  • How fixtures are made available to tests in multiple files without importing them explicitly

Part 2 — Decorators

Explain what a Python decorator is and what the @some_decorator line above a function definition actually does at runtime. Then write a decorator called timed that:

  • Records how long the decorated function takes to run
  • Prints or logs the elapsed time whether the call succeeds or raises an exception
  • Forwards all positional and keyword arguments to the original function and returns its result unchanged
  • Preserves the original function's __name__ and __doc__

Also briefly explain how you would modify timed so it accepts its own configuration argument, for example @timed(unit="ms").


Part 3 — Generators

Explain what a Python generator is and how it differs from a regular function that builds and returns a list. Cover:

  • What yield does to a function's execution — how it suspends, what gets preserved, and how the caller resumes it
  • The iterator protocol a generator object implements
  • At least two scenarios where a generator is clearly preferable to a list, and one scenario where a list is the better choice

Then write a generator function matching_lines(path, substring) that opens a log file, yields only the lines that contain substring, and ensures the file is closed correctly even if the caller stops iterating early or an exception occurs. Do not read the entire file into memory at once.

Hints

Hint 1

For Part 1, remember that a fixture's scope must be equal to or broader than the scope of any fixture it depends on — pytest will raise an error otherwise. Think about what conftest.py files are for.

Hint 2

For Part 2, applying @functools.wraps(func) inside your wrapper is what copies __name__, __doc__, and other metadata. To support decorator arguments, you need an extra layer of nesting — a function that returns the actual decorator.

Hint 3

For Part 3, a generator function paired with a try/finally block (or a with statement) is the cleanest way to guarantee the file closes even when the caller breaks out of iteration early — consider what happens to an open file handle if the generator is abandoned mid-way.