You are currently viewing Top 20 Pytest Interview Questions with Answers [2025 Guide]
Top 20 Pytest Interview Questions with Answers [2025 Guide]

Top 20 Pytest Interview Questions with Answers [2025 Guide]

If you’re preparing for a Python automation testing or QA interview, mastering Pytest is non-negotiable. It’s one of the most widely used testing frameworks in Python, loved for its simplicity, scalability, and rich plugin support.

Whether you’re a beginner, job seeker, or experienced QA engineer, these top 20 Pytest interview questions with answers will help you confidently crack your next technical round.

🔹 1. What is Pytest?

Answer:
Pytest is a powerful Python testing framework used for unit, functional, and end-to-end testing. It supports fixtures, parameterization, and parallel test execution, making it ideal for scalable automation projects.


🔹 2. How do you install Pytest?

Answer:
You can install Pytest using pip:

pip install pytest

🔹 3. How do you write a simple test case using Pytest?

Answer:

def test_addition():
    assert 2 + 2 == 4

Any function that starts with test_ is automatically detected by Pytest.


🔹 4. How do you run tests using Pytest?

Answer:
Run all tests in a directory:

pytest

Run a specific test file:

pytest test_example.py

🔹 5. What are Pytest fixtures?

Answer:
Fixtures provide a way to set up preconditions and test data. They use the @pytest.fixture decorator.

@pytest.fixture
def sample_data():
    return {"name": "Raj", "age": 30}

🔹 6. What is the scope of fixtures in Pytest?

Answer:
Fixture scopes can be:

  • function (default)
  • class
  • module
  • session

🔹 7. How do you parametrize test cases in Pytest?

Answer:

import pytest

@pytest.mark.parametrize("a, b, result", [(2, 3, 5), (1, 1, 2)])
def test_add(a, b, result):
assert a + b == result

🔹 8. What is conftest.py in Pytest?

Answer:
conftest.py is a special configuration file where you define shared fixtures, hooks, and plugins. It’s automatically detected by Pytest.


🔹 9. How do you skip a test case in Pytest?

Answer:

import pytest

@pytest.mark.skip(reason="Under development")
def test_feature():
    assert True

🔹 10. How do you mark tests in Pytest?

Answer:

@pytest.mark.smoke
def test_login():
    assert True

Run marked tests:

pytest -m smoke

🔹 11. How do you run failed tests only?

Answer:
Use the --lf flag:

pytest --lf

🔹 12. How do you generate HTML test reports in Pytest?

Answer:
Install the plugin:

pip install pytest-html

Run with:

pytest --html=report.html

🔹 13. How do you handle exceptions in test cases?

Answer:

import pytest

def raise_error():
    raise ValueError("Invalid")

def test_error():
    with pytest.raises(ValueError):
        raise_error()

🔹 14. How do you run tests in parallel with Pytest?

Answer:
Install the plugin:

pip install pytest-xdist

Run:

test -n 4

🔹 15. Can you run setup and teardown logic in Pytest?

Answer:
Use setup_function, teardown_function, or fixtures with yield.

@pytest.fixture
def setup_teardown():
    print("Setup")
    yield
    print("Teardown")

🔹 16. How does Pytest discover test files?

Answer:
By default, Pytest looks for:

  • Files starting with test_
  • Functions starting with test_
  • Classes prefixed with Test

🔹 17. What’s the difference between assert and pytest.fail()?

Answer:

  • assert checks conditions.
  • pytest.fail() explicitly fails the test with a custom message.

🔹 18. How do you integrate Pytest with CI/CD pipelines?

Answer:
Use commands like pytest --junitxml=results.xml to generate reports, and configure your CI tool (GitHub Actions, Jenkins, etc.) to use these results.


🔹 19. Can you mock APIs or functions in Pytest?

Answer:
Yes, using the unittest.mock or pytest-mock plugin.

from unittest.mock import Mock

def test_mock_example():
    m = Mock()
    m.return_value = 10
    assert m() == 10

🔹 20. What are some popular Pytest plugins?

Answer:

  • pytest-html (for reports)
  • pytest-xdist (parallel execution)
  • pytest-mock (mocking support)
  • pytest-django, pytest-flask (for frameworks)

✅ Final Thoughts

These top 20 Pytest interview questions cover the most commonly asked concepts in automation testing roles across startups and enterprises. Whether you’re just starting your testing career or preparing for a QA position, Pytest is a must-have skill in your toolkit.


📌 Bonus Resources:

Leave a Reply