Pytest Interview Questions
Check out 30 of the most common Pytest interview questions and take an AI-powered practice interview
What is Pytest and why is it preferred over unittest?
How does Pytest discover tests automatically?
What is the assert statement in Pytest and how does assertion rewriting work?
What is a Pytest fixture and how do you define one?
What are the different fixture scopes and when should you use each?
What is conftest.py and how is it used?
How do you run a specific test, file, or test method in Pytest?
What is parametrization in Pytest and how do you use @pytest.mark.parametrize?
What are markers in Pytest? Explain skip, skipif, and xfail.
How do you test that a function raises an exception?
What is autouse in fixtures and when should you use it?
How do you capture stdout/stderr in a Pytest test?
How does fixture scope cascading work, and what happens if scopes mismatch?
How do you use pytest-mock and what's the difference from unittest.mock?
Where should you patch — at the source or at the import site? Why is this a common bug?
How do you write async tests with pytest-asyncio?
How do you measure test coverage with pytest-cov?
How do you run tests in parallel with pytest-xdist?
How do you parametrize a fixture?
What is indirect parametrization and when is it useful?
How do you share state between tests deliberately? When is it OK?
How do you test database code with Pytest?
What is pytest-django and how does it differ from Django's TestCase?
How do you use tmp_path and tmp_path_factory?
How does monkeypatch work and when should you use it over mocker?
How do you write a Pytest plugin and what are the most useful hooks?
How would you design tests for an async LLM-streaming API in 2026?
How do you debug a flaky test in Pytest?
How do you organize a large test suite (10,000+ tests) for fast feedback?
What's the difference between Pytest hooks `pytest_collection_modifyitems`, `pytest_runtest_setup`, and `pytest_runtest_call`? When does each fire?
Frequently Asked Questions
Is Pytest worth learning in 2026 when AI can write tests?
Yes — and arguably more so. AI assistants now generate first-pass test code, but you still need to know the framework deeply to review, debug failures, and design the fixture architecture for a project. Every Python team in India still hires for Pytest fluency in 2026; it's table-stakes for a backend or ML engineering role.
How much does a Python developer with strong Pytest skills earn in India?
₹5-18 LPA in 2026, depending on experience. Mid-level backend or data engineers at Razorpay, Swiggy, Zomato, PhonePe, and similar startups typically expect Pytest fluency. Strong test design — fixtures, mocking, plugins — is a frequent differentiator at the senior level.
Should I use unittest or Pytest for a new Python project?
Pytest, in almost every case. unittest is fine for tiny scripts where adding a dependency is unwelcome, but for any project with more than ~20 tests, Pytest's fixture system, parametrization, plugin ecosystem, and assertion rewriting save more time than the install cost. Pytest can also run unittest-style tests, so migration is incremental.
What Pytest version should I be on in 2026?
Pytest 8.x is the current stable line — works with Python 3.9+ and ships modern fixtures, improved assertion rewriting, and faster collection. Major plugins (pytest-asyncio, pytest-cov, pytest-django, pytest-xdist, pytest-mock) all support it.
How do I handle tests that hit external APIs (OpenAI, Razorpay, etc.)?
Three options: (1) Mock the SDK in unit tests using pytest-mock — fast and deterministic. (2) Use `vcrpy` or `pytest-recording` to record real responses once and replay them — catches contract drift without paying per CI run. (3) Reserve real-API tests for a nightly or pre-release pipeline. Never run paid-API tests on every PR — the cost adds up fast.
Introduction
Pytest is the de-facto standard test framework in the Python world in 2026. It has completely displaced the standard-library `unittest` for new projects and is the testing backbone of every major Python codebase — from Instagram and Dropbox to Razorpay and PhonePe. Its appeal: plain `assert` statements, function-style tests (no classes required), and a fixture system that's both powerful and surprisingly easy to misuse.
If you're interviewing for any Python role in India today — backend, data engineering, ML engineering, DevOps — expect Pytest questions. They range from 'what's the difference between a fixture and a parametrized test' to 'how do you mock a class method that's imported into another module' (a classic gotcha that trips up even senior engineers).
This guide covers the 30 most-asked Pytest interview questions in 2026, grouped by difficulty. Each answer includes the underlying concept, a working code example where it adds clarity, and the real-world gotchas hiring managers expect you to know about.