Testing & Test Frameworks Interview Questions
Comprehensive testing & test frameworks interview questions and answers for Python. Prepare for your next job interview with expert guidance.
Questions Overview
1. What are the main differences between unittest and pytest frameworks?
Basic2. How do fixtures work in pytest and what are their benefits?
Moderate3. What is mocking and how is it implemented in Python tests?
Moderate4. How do you measure and improve test coverage?
Basic5. What is Test-Driven Development (TDD) and how is it practiced?
Basic6. How do you implement parameterized testing in pytest?
Moderate7. What are pytest markers and how are they used?
Moderate8. How do you handle database testing?
Advanced9. What is the purpose of test doubles (mocks, stubs, fakes)?
Moderate10. How do you test async code in Python?
Advanced11. What are best practices for test organization?
Basic12. How do you implement integration testing?
Advanced13. What is property-based testing and how is it implemented?
Advanced14. How do you handle test data management?
Moderate15. What are pytest conftest.py files and their purpose?
Moderate16. How do you implement performance testing?
Advanced17. What is monkey patching and when should it be used?
Advanced18. How do you test exception handling?
Moderate19. What are pytest plugins and how are they used?
Moderate20. How do you implement API testing?
Advanced21. What is behavior-driven development (BDD) in Python?
Advanced22. How do you handle environment-specific testing?
Moderate23. What are testing anti-patterns to avoid?
Advanced24. How do you implement concurrent test execution?
Advanced25. What are the strategies for testing logging?
Moderate26. How do you implement security testing?
Advanced27. What are fixtures scope levels and when to use each?
Moderate28. How do you implement continuous testing?
Moderate29. What are the patterns for testing GUI applications?
Advanced1. What are the main differences between unittest and pytest frameworks?
Basicunittest is built-in, class-based, requires test classes inheriting from TestCase. pytest is more flexible, supports function-based tests, better fixtures, parametrization, and plugins. pytest has more powerful assertions and better error reporting. Consider project needs for framework choice.
2. How do fixtures work in pytest and what are their benefits?
ModerateFixtures provide reusable test setup/teardown, defined using @pytest.fixture decorator. Support dependency injection, different scopes (function, class, module, session). Enable clean test organization, resource sharing. Example: database connections, test data setup.
3. What is mocking and how is it implemented in Python tests?
ModerateMocking replaces real objects with test doubles. Use unittest.mock or pytest-mock. MagicMock/Mock classes provide automatic attribute creation. Common uses: external services, databases, file operations. Consider patch decorator/context manager.
4. How do you measure and improve test coverage?
BasicUse coverage.py or pytest-cov. Run tests with coverage collection, generate reports. Analyze uncovered lines, branches. Set minimum coverage requirements. Consider meaningful vs. superficial coverage. Focus on critical paths.
5. What is Test-Driven Development (TDD) and how is it practiced?
BasicTDD cycle: write failing test, write code to pass, refactor. Tests drive design, document requirements. Write minimal code to pass tests. Benefits: better design, regression protection, documentation. Consider Red-Green-Refactor cycle.
6. How do you implement parameterized testing in pytest?
ModerateUse @pytest.mark.parametrize decorator to run same test with different inputs. Supports multiple parameters, custom IDs. Reduces test code duplication. Example: @pytest.mark.parametrize('input,expected', [(1,2), (2,4)]). Consider data organization.
7. What are pytest markers and how are they used?
ModerateMarkers (@pytest.mark) categorize tests, control execution. Built-in markers: skip, skipif, xfail. Custom markers for test organization, selection. Register markers in pytest.ini. Consider marker documentation, organization.
8. How do you handle database testing?
AdvancedUse test databases, fixtures for setup/teardown. Consider transaction rollback, database isolation. Mock database when appropriate. Implement proper cleanup. Use tools like pytest-django for framework-specific support.
9. What is the purpose of test doubles (mocks, stubs, fakes)?
ModerateTest doubles replace real dependencies. Mocks verify interactions, stubs provide canned responses, fakes implement lightweight alternatives. Choose based on test needs. Consider interaction vs. state testing.
10. How do you test async code in Python?
AdvancedUse pytest-asyncio for async tests. Mark tests with @pytest.mark.asyncio. Handle coroutines properly. Consider event loop management. Test async contexts, timeouts. Handle async cleanup properly.
11. What are best practices for test organization?
BasicGroup related tests, use clear naming conventions. Separate unit/integration tests. Follow AAA pattern (Arrange-Act-Assert). Maintain test independence. Consider test discoverability, maintenance.
12. How do you implement integration testing?
AdvancedTest component interactions, external services. Use appropriate fixtures, mocking selectively. Consider test environment setup. Handle cleanup properly. Balance coverage vs. execution time.
13. What is property-based testing and how is it implemented?
AdvancedUse hypothesis library for property-based testing. Define properties, let framework generate test cases. Useful for finding edge cases. Consider strategy definition, test case generation. Handle test case reduction.
14. How do you handle test data management?
ModerateUse fixtures, factory libraries (factory_boy). Consider data isolation, cleanup. Implement proper test data generation. Handle complex data relationships. Consider data versioning, maintenance.
15. What are pytest conftest.py files and their purpose?
Moderateconftest.py provides shared fixtures across multiple test files. Defines test configuration, custom markers. Supports fixture overriding, plugin hooks. Consider scope organization, reusability.
16. How do you implement performance testing?
AdvancedUse pytest-benchmark for performance tests. Measure execution time, resource usage. Consider baseline comparisons, statistical analysis. Handle environment variations. Document performance requirements.
17. What is monkey patching and when should it be used?
AdvancedMonkey patching modifies objects/modules at runtime for testing. Use pytest.monkeypatch fixture. Handle cleanup properly. Consider implications on test isolation. Use sparingly, prefer dependency injection.
18. How do you test exception handling?
ModerateUse pytest.raises context manager or unittest.assertRaises. Test exception types, messages. Consider exception inheritance, multiple exceptions. Test cleanup handling. Verify exception context.
19. What are pytest plugins and how are they used?
ModeratePlugins extend pytest functionality. Common plugins: pytest-cov, pytest-mock, pytest-django. Install via pip, configure in pytest.ini. Consider plugin interactions, maintenance. Document plugin requirements.
20. How do you implement API testing?
AdvancedUse requests, pytest-httpx for HTTP testing. Mock external services appropriately. Consider response validation, error cases. Handle authentication, rate limiting. Test different HTTP methods.
21. What is behavior-driven development (BDD) in Python?
AdvancedUse pytest-bdd or behave for BDD. Write tests in Gherkin syntax. Map steps to test code. Consider stakeholder communication. Balance readability vs. maintenance. Document behavior specifications.
22. How do you handle environment-specific testing?
ModerateUse environment variables, configuration files. Implement test environment management. Consider CI/CD integration. Handle sensitive data properly. Document environment requirements.
23. What are testing anti-patterns to avoid?
AdvancedAvoid test interdependence, slow tests, excessive mocking. Don't test implementation details. Avoid non-deterministic tests. Consider maintenance cost. Document test assumptions clearly.
24. How do you implement concurrent test execution?
AdvancedUse pytest-xdist for parallel testing. Consider test isolation, shared resources. Handle race conditions. Implement proper cleanup. Balance parallelism vs. resource usage.
25. What are the strategies for testing logging?
ModerateUse caplog fixture in pytest. Verify log messages, levels. Consider log handlers, formatting. Test logger configuration. Handle temporary logger modifications.
26. How do you implement security testing?
AdvancedTest input validation, authentication, authorization. Use security testing tools (bandit). Consider vulnerability scanning. Test security configurations. Document security requirements.
27. What are fixtures scope levels and when to use each?
ModerateScopes: function (default), class, module, session. Choose based on resource costs, test isolation needs. Consider cleanup timing. Handle dependencies between fixtures. Document scope requirements.
28. How do you implement continuous testing?
ModerateIntegrate tests in CI/CD pipeline. Automate test execution, reporting. Consider test selection, prioritization. Handle test failures appropriately. Document test requirements.
29. What are the patterns for testing GUI applications?
AdvancedUse PyTest-Qt, PyAutoGUI for GUI testing. Handle event loops properly. Consider screenshot comparisons. Test user interactions. Handle window management. Document visual requirements.