Interview Questions

Get ready for your next interview with our comprehensive question library

PyTest Interview Questions

Filter by Difficulty

1.

What is PyTest and how does it differ from unittest?

beginner

PyTest is a mature, feature-rich Python testing framework that makes writing tests simple and scalable.

Key differences from unittest:

  • Simpler syntax: Uses plain assert statements vs self.assertEqual()
  • Automatic test discovery: No need for test suites
  • Rich plugin ecosystem: Extensive third-party support
  • Fixtures: Powerful dependency injection system
  • Parametrized testing: Built-in multiple input support
  • Better error reporting: More detailed failure messages
# PyTest style
def test_addition():
    assert 2 + 2 == 4

# unittest style
import unittest
class TestMath(unittest.TestCase):
    def test_addition(self):
        self.assertEqual(2 + 2, 4)
2.

How do you install and run PyTest?

beginner

Installation via pip:

pip install pytest

Running tests:

pytest                    # Run all tests
pytest test_file.py       # Run specific file
pytest -v                 # Verbose output
pytest -k "test_name"     # Run tests matching pattern
3.

What are the naming conventions for PyTest test discovery?

beginner

PyTest automatically discovers tests following these conventions:

  • Test files: test_*.py or *_test.py
  • Test functions: Functions starting with test_
  • Test classes: Classes starting with Test (no __init__ method)
  • Test methods: Methods in test classes starting with test_
# Valid test file: test_math.py
def test_addition():        # ✓ Discovered
    pass
class TestCalculator:       # ✓ Discovered
    def test_multiply(self): # ✓ Discovered
        pass
    def helper_method(self): # ✗ Not discovered
        pass
4.

How do you skip tests in PyTest?

beginner

PyTest provides several ways to skip tests:

import pytest
@pytest.mark.skip(reason="Not implemented yet")
def test_feature():
    pass
@pytest.mark.skipif(sys.version_info < (3, 8), reason="Requires Python 3.8+")
def test_walrus_operator():
    pass
def test_conditional_skip():
    if not has_database_connection():
        pytest.skip("Database not available")
5.

What is the difference between test modules, test classes, and test functions?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
6.

What are fixtures in PyTest and why are they useful?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
7.

How do you test for exceptions in PyTest?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
8.

How does PyTest's test discovery mechanism work?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
9.

How can you organize tests in different directories?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
10.

What are the different fixture scopes in PyTest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

How do you create and use fixture dependencies?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

What is the difference between `yield` and `return` in fixtures?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

How do you use `conftest.py` files?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

How do you create parametrized tests in PyTest?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

How can you parametrize fixtures?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

What are markers in PyTest and how do you use them?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

How do you create custom markers?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

What is `pytest.ini` and what can you configure in it?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What are PyTest's built-in assertion helpers?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How do you use mocking in PyTest tests?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 33 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime