🐍 Python Intermediate

What is pytest in Python?

Why Interviewers Ask This

This question targets practical, hands-on experience with Python. Interviewers want to see if you've worked with these concepts in real projects, not just read about them. Strong answers include concrete examples.

Answer

pytest is the most popular Python testing framework. Tests are simple functions starting with test_, using plain assert statements: def test_add(): assert add(2, 3) == 5. Run: pytest (discovers tests automatically). Fixtures provide reusable setup/teardown: @pytest.fixture def db(): conn = create_connection(); yield conn; conn.close() — inject into tests by name. Fixture scopes: function, class, module, session. Parameterize: @pytest.mark.parametrize("x,y,result", [(1,2,3),(4,5,9)]). Marks: @pytest.mark.skip, @pytest.mark.xfail, custom marks. Plugins: pytest-mock (mocking), pytest-cov (coverage), pytest-asyncio (async tests), pytest-django. Pytest automatically provides detailed failure diffs and does not require subclassing TestCase.

Pro Tip

Back up your answer with a specific project or situation. Saying 'In my last Python project, I used this when...' immediately makes your answer more credible and memorable.