Technical Entry Level

How do you structure tests in a Python project? Compare pytest with unittest and explain your approach to fixtures and mocking.

Quick Tip

Describe your setup: "I use pytest with fixtures scoped per function for isolation. Parametrize replaces duplicate test methods. I patch external services but prefer dependency injection for internal code."

What good answers include

pytest is the community standard: less boilerplate than unittest, powerful fixtures with dependency injection, parametrize for table-driven tests, and a rich plugin ecosystem. Fixtures replace setUp/tearDown with explicit, composable, scoped resources. Mocking: unittest.mock.patch for replacing dependencies, or dependency injection to avoid patching entirely. Strong candidates discuss test organisation (unit/integration/e2e directories), conftest.py for shared fixtures, and markers for slow tests.

What interviewers are looking for

Fundamental Python testing knowledge. Candidates who only know unittest may be behind on ecosystem conventions. Those who can discuss fixture scoping, parametrize, and test organisation demonstrate professional testing practices.

← All Python questions