Q1. What is Pytest, and why is it used in Python projects?
Pytest is a Python testing framework used to write simple and scalable test cases.
It supports automated testing for functions, APIs, and applications. Pytest requires less boilerplate code compared to unittest.
It automatically discovers test files and functions. Pytest is widely used because of its simplicity, powerful features, and strong plugin ecosystem.
Q2. How does Pytest discover test files and test functions?
Pytest automatically discovers tests based on naming conventions.
Test files should start with test_ or end with _test.py. Test functions must also begin with test_.
Pytest scans directories recursively to locate tests. This automatic discovery removes the need for manual test registration.
Q3. How does Pytest execute test cases and report results?
Pytest runs test cases sequentially or in parallel (with plugins).
It executes test functions and checks assertions. If an assertion fails, the test is marked as failed.
Pytest provides detailed error output showing expected vs actual values. This makes debugging faster and easier.
Q4. How do fixtures work in Pytest?
Fixtures provide reusable setup and teardown logic for tests. They prepare test data, database connections, or configurations.
Fixtures are defined using the @pytest.fixture decorator. Tests request fixtures as function arguments. This promotes clean, modular, and maintainable test code.
Q5. Difference between Pytest and unittest
| Feature | Pytest | unittest |
| Boilerplate | Minimal | More |
| Test Discovery | Automatic | Manual |
| Assertions | Plain assert | Special methods |
| Popularity | Very high | Moderate |
Pytest is preferred for modern Python testing due to simplicity.
Q6. Difference between setup/teardown and fixtures
| Aspect | setup/teardown | Fixtures |
| Reusability | Low | High |
| Scope control | Limited | Flexible |
| Readability | Lower | Better |
| Pytest Style | Old | Recommended |
Fixtures are the recommended approach in Pytest.
Q7. Difference between assert and pytest.raises()
| Feature | assert | pytest.raises() |
| Purpose | Check values | Check exceptions |
| Use Case | Output validation | Error handling |
| Syntax | Simple | Context-based |
| Example | assert a == b | with raises() |
Both are essential for writing robust tests.
Q8. Difference between skip and xfail
| Feature | skip | xfail |
| Meaning | Do not run test | Expected to fail |
| Reason | Not applicable | Known bug |
| Result | Skipped | XFailed |
| Usage | Temporary | Planned fix |
These markers help manage unstable tests.
Q9. How do you install Pytest?
Pytest is installed using pip with pip install pytest. It works across platforms.
After installation, tests are run using the pytest command. Pytest integrates well with virtual environments. Installation is simple and fast.
Q10. How do you run Pytest tests?
Tests are run by executing the pytest command in the project directory.
Pytest automatically finds and runs tests. You can run specific files or tests using filenames or test names.
Command-line options customize execution. This flexibility is interview-relevant.
Q11. What is a test case in Pytest?
A test case is a function that verifies a specific behavior. It contains assertions to validate output.
Test cases should be independent and repeatable. Good test cases focus on one functionality. Pytest makes writing test cases easy.
Q12. What is assertion in Pytest?
Assertions validate expected behavior. Pytest uses Python’s built-in assert statement. If the condition fails, Pytest shows a detailed error message. Assertions improve test reliability. They are the core of test validation.
Q13. What are Pytest markers?
Markers categorize tests. Common markers include skip, xfail, and custom markers. They control which tests run under certain conditions. Markers help organize large test suites. They improve test management.
Q14. What is conftest.py?
conftest.py stores shared fixtures and configurations. It allows reuse across multiple test files.
Pytest automatically loads it. No imports are required. It helps maintain clean test structures.
Q15. What is parameterized testing in Pytest?
Parameterized testing runs the same test with different inputs. It reduces code duplication.
Pytest provides @pytest.mark.parametrize. It improves coverage and readability. It’s commonly used in real projects.
Q16. How do you test exceptions in Pytest?
Exceptions are tested using pytest.raises(). It ensures code raises expected errors. This validates error-handling logic.
Exception testing is essential for robust applications. Interviews often ask this.
Q17. What is test coverage?
Test coverage measures how much code is tested. Tools like pytest-cov are used. High coverage reduces bugs. Coverage helps identify untested code. It improves software quality.
Q18. How does Pytest help in CI/CD pipelines?
Pytest integrates easily with CI tools like GitHub Actions and Jenkins. Tests run automatically on commits. This prevents broken code from reaching production. Pytest supports fast feedback loops. CI integration is very common in interviews.
Q19. What is mocking in Pytest?
Mocking simulates external dependencies. It isolates code under test. Pytest uses unittest.mock or plugins. Mocking improves test reliability. It avoids real API or DB calls.
Q20. Why is Pytest preferred in modern Python projects?
Pytest is simple, powerful, and flexible. It reduces boilerplate code. It supports fixtures, plugins, and parameterization. Pytest improves test readability and maintainability. This makes it the top choice for Python testing.





