PyTest Python Testing Interview
Curated 20+ questions covering PyTest assertions, fixtures, yield teardowns, parameterization, markers, and plugins.
Difficulty Filters
Tools & Options
What is PyTest, and what makes it preferred over Python's built-in `unittest` library?
PyTest is a robust, open-source Python testing framework. Key advantages over `unittest` include:
1. Simple syntax: No need to wrap tests in classes; tests are simple functions prefixed with `test_`.
2. Plain `assert` statements: No need for helper methods like `self.assertEqual()`.
3. Powerful Fixtures: High-speed modular dependency sharing.
4. Rich plugin ecosystem.
How does PyTest handle assertions? What is assertion introspection?
PyTest allows using Python's standard `assert` keyword. During test runs, PyTest uses assertion introspection to rewrite assertion statements, capturing detailed execution outputs and displaying values of variables on failures.
What are PyTest Fixtures, and how are they defined?
Fixtures are helper functions that run before tests to set up databases, configure clients, or prepare mock variables. They are defined by annotating functions with `@pytest.fixture`. Tests consume them by listing the fixture name as a parameter.
import pytest
@pytest.fixture
def db_connection():
db = connect()
return db
def test_query(db_connection):
result = db_connection.query("SELECT...")
assert result is not NoneWhat is `conftest.py` in PyTest?
Explain the scope of a PyTest Fixture.
How do you perform teardown or cleanup actions in a PyTest Fixture?
Explain Parameterized Tests in PyTest.
What are PyTest Markers?
How do you test that a function raises an exception in PyTest?
What is PyTest autouse fixture?
How do you execute specific tests in PyTest?
What is `pytest.ini`?
What is the difference between `pytest.fixture(scope="session")` and global variables?
How do you mock or patch dependencies in PyTest?
What is the difference between `xfail` and `skip` markers?
Explain PyTest fixtures dependency injection (fixtures using other fixtures).
What is the `capfd` or `capsys` fixture in PyTest?
How do you generate HTML test reports in PyTest?
What are pytest plugins? List common ones.
Explain the `pytest-xdist` plugin and parallel execution.
Technical Interview Mastery Blueprint
Proven methodologies and frameworks to succeed in technical and architecture rounds.
1. The Engineering STAR Technique
When responding to architectural or situational questions, structure your answer using Situation, Technical Constraint, Action, and Measurable Result. Rather than jumping straight to syntax, articulate the business problem, why certain patterns (e.g. caching, queuing, immutability) were selected over alternatives, and what latency or throughput improvements were achieved.
2. Defending Architectural Trade-Offs
Senior interviewers evaluate how you weigh trade-offs. Always explain why you chose an approach: e.g., memory overhead vs CPU cycle speed, consistency vs availability (CAP theorem), or normalized schemas vs denormalized fast reads. Stating the drawbacks of your own proposal demonstrates maturity and production experience.
3. Clean Code & Edge-Case Proactivity
In live-coding rounds, communicate your thinking aloud before typing. State edge cases explicitly: null or undefined inputs, extreme boundary values, concurrency races, and network timeouts. Writing modular, well-named functions with deterministic unit tests separates junior candidates from staff-level engineers.
4. Antipatterns to Avoid
Avoid reciting memorized definitions without practical context. Never dismiss non-functional requirements such as security (CSRF, injection, token rotation), observability (logging, metrics), and accessibility (ARIA, semantic standards). Always ask clarifying scope questions before writing complex implementations.
Explore Related Tools
Hand-picked utilities and calculators related to this tool.
