Pytest Interview Questions

Check out 30 of the most common Pytest interview questions and take an AI-powered practice interview

PythonFixturesParametrizationPluginsMarkers
30+
Questions
12
Basic
13
Intermediate
5
Advanced
Q1

What is Pytest and why is it preferred over unittest?

BasicFundamentals
+
Q2

How does Pytest discover tests automatically?

BasicTest Discovery
+
Q3

What is the assert statement in Pytest and how does assertion rewriting work?

BasicAssertions
+
Q4

What is a Pytest fixture and how do you define one?

BasicFixtures
+
Q5

What are the different fixture scopes and when should you use each?

BasicFixtures
+
Q6

What is conftest.py and how is it used?

BasicFixtures
+
Q7

How do you run a specific test, file, or test method in Pytest?

BasicCLI
+
Q8

What is parametrization in Pytest and how do you use @pytest.mark.parametrize?

BasicParametrization
+
Q9

What are markers in Pytest? Explain skip, skipif, and xfail.

BasicMarkers
+
Q10

How do you test that a function raises an exception?

BasicAssertions
+
Q11

What is autouse in fixtures and when should you use it?

BasicFixtures
+
Q12

How do you capture stdout/stderr in a Pytest test?

BasicBuilt-in Fixtures
+
Q13

How does fixture scope cascading work, and what happens if scopes mismatch?

IntermediateFixtures
+
Q14

How do you use pytest-mock and what's the difference from unittest.mock?

IntermediateMocking
+
Q15

Where should you patch — at the source or at the import site? Why is this a common bug?

IntermediateMocking
+
Q16

How do you write async tests with pytest-asyncio?

IntermediateAsync
+
Q17

How do you measure test coverage with pytest-cov?

IntermediateCoverage
+
Q18

How do you run tests in parallel with pytest-xdist?

IntermediatePerformance
+
Q19

How do you parametrize a fixture?

IntermediateParametrization
+
Q20

What is indirect parametrization and when is it useful?

IntermediateParametrization
+
Q21

How do you share state between tests deliberately? When is it OK?

IntermediateFixtures
+
Q22

How do you test database code with Pytest?

IntermediateDatabase Testing
+
Q23

What is pytest-django and how does it differ from Django's TestCase?

IntermediatePlugins
+
Q24

How do you use tmp_path and tmp_path_factory?

IntermediateBuilt-in Fixtures
+
Q25

How does monkeypatch work and when should you use it over mocker?

IntermediateMocking
+
Q26

How do you write a Pytest plugin and what are the most useful hooks?

AdvancedPlugins
+
Q27

How would you design tests for an async LLM-streaming API in 2026?

AdvancedAsync
+
Q28

How do you debug a flaky test in Pytest?

AdvancedDebugging
+
Q29

How do you organize a large test suite (10,000+ tests) for fast feedback?

AdvancedArchitecture
+
Q30

What's the difference between Pytest hooks `pytest_collection_modifyitems`, `pytest_runtest_setup`, and `pytest_runtest_call`? When does each fire?

AdvancedPlugins
+

Companies Hiring Pytest

Instagram
Dropbox
Mozilla
Razorpay
Zomato
PhonePe
Swiggy

Salary Insights

Average in India
₹5-18 LPA

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.