Pytest Testing Framework
Pytest is a popular Python testing framework that makes it easy to write simple and scalable test cases.
Key Features
Test Discovery
- Automatically finds test files and functions
- Naming convention:
test_*.pyfiles andtest_*functions - Supports custom test discovery patterns
Fixtures
- Reusable test components
- Dependency injection for tests
- Scope management (function, class, module, session)
- Parametrized fixtures
Assertions
- Rich assertion introspection
- Detailed failure messages
- No need for special assertion methods
Common Interview Questions
- What are the advantages of pytest over unittest?
- Explain pytest fixtures and their scopes
- How to parametrize tests in pytest?
- What are pytest markers and how to use them?
- How to handle test setup and teardown?
- Explain pytest plugins and their usage
- How to run specific tests or test groups?
- What is pytest-xdist and when to use it?
- How to mock objects in pytest?
- Explain pytest configuration (pytest.ini, conftest.py)
- How to generate test coverage reports?
- What are pytest hooks and how to use them?