All interview guides

Pytest Interview Questions and Answers

24 questions that come up in Pytest technical interviews, each with the answer and an explanation of why it is right.

Test yourself — 90 question bank

1. What are pytest hooks?

Advanced

Answer: Extension points for customizing pytest behavior

Hooks are functions pytest calls at specific points. Define in conftest.py or plugins. Examples: pytest_configure, pytest_collection_modifyitems. Powerful customization.

2. What is Pytest?

Beginner

Answer: Python testing framework with simple syntax

Pytest is a mature, feature-rich Python testing framework. It provides simple syntax with powerful features like fixtures, parametrization, and plugins.

3. What is parametrize?

Intermediate

Answer: Runs test with multiple input sets

@pytest.mark.parametrize runs test with different inputs. Creates separate test per input set. Clear reporting per case. Essential for data-driven testing.

4. How do you use parametrize?

Intermediate

Answer: @pytest.mark.parametrize('args', [values])

@pytest.mark.parametrize("x,y,expected", [(1,2,3), (2,3,5)]). First arg is parameter names, second is value tuples. Multiple parameters comma-separated.

5. How do you install Pytest?

Beginner

Answer: pip install pytest

Install Pytest with pip install pytest. Use pip install -U pytest to upgrade. Check version with pytest --version. Also available via conda.

6. What is pytest_collection_modifyitems?

Advanced

Answer: Hook to modify collected test items

pytest_collection_modifyitems(config, items) modifies collected tests. Reorder, filter, mark tests. Runs after collection. Useful for dynamic test configuration.

7. What is the naming convention for test files?

Beginner

Answer: test_*.py or *_test.py

Test files must start with test_ or end with _test.py. Examples: test_example.py, example_test.py. Pytest auto-discovers these files.

8. What is pytest_configure?

Advanced

Answer: Hook called after config is initialized

pytest_configure(config) runs after config initialized. Register plugins, markers. Access config. Early customization point. Runs once per session.

9. What are fixture scopes?

Intermediate

Answer: function, class, module, package, session

Fixture scopes: function (default), class, module, package, session. Determines when fixture is setup/torn down. Broader scope = shared across more tests.

10. What is pytest_runtest_setup?

Advanced

Answer: Hook called before test execution

pytest_runtest_setup(item) runs before each test. Modify test environment. Runs before fixtures. Access test item metadata. Per-test customization.

11. What is the naming convention for test functions?

Beginner

Answer: Must start with test_

Test functions must start with test_. Example: def test_addition(). Test classes must start with Test (no __init__). Pytest auto-discovers these.

12. What is fixture autouse?

Intermediate

Answer: Automatically applies fixture to all tests

@pytest.fixture(autouse=True) applies automatically to all tests in scope. No need to request explicitly. Useful for setup/teardown that always applies.

13. How do you create a custom plugin?

Advanced

Answer: Create module with hooks and register

Create module with pytest hooks. Register: pytest_plugins = ["mymodule"] in conftest.py or setup.py entry_points. Distribute via pip. Reusable customizations.

14. What is yield in fixtures?

Intermediate

Answer: Separates setup from teardown code

yield in fixtures separates setup (before yield) from teardown (after yield). @pytest.fixture def resource(): setup(); yield value; teardown(). Cleaner than return.

15. How do you run all tests?

Beginner

Answer: Both a and b

Run tests with pytest or python -m pytest. Both work. Pytest discovers and runs all test files in current directory and subdirectories.

16. What is fixture dependency?

Intermediate

Answer: Fixtures can depend on other fixtures

Fixtures can depend on other fixtures by requesting them as parameters. Creates dependency graph. Pytest manages order automatically. Enables composition.

17. How do you run a specific test file?

Beginner

Answer: pytest test_file.py

Run specific file: pytest test_file.py. Can specify multiple files. Use path/to/test_file.py for files in subdirectories.

18. What is pytest_addoption?

Advanced

Answer: Hook to add custom command-line options

pytest_addoption(parser) adds CLI options. parser.addoption("--myopt"). Access with config.getoption("myopt"). Define custom test behavior via CLI.

19. How do you run a specific test function?

Beginner

Answer: pytest test_file.py::test_function

Run specific test: pytest test_file.py::test_function. Use :: separator. Can also specify class: pytest test_file.py::TestClass::test_method.

20. What is pytest_generate_tests?

Advanced

Answer: Hook for custom parametrization logic

pytest_generate_tests(metafunc) enables custom parametrization. metafunc.parametrize(). More flexible than @pytest.mark.parametrize. Dynamic test generation.

21. What are markers?

Intermediate

Answer: Metadata tags for tests

Markers are metadata tags: @pytest.mark.slow, @pytest.mark.integration. Filter tests: pytest -m slow. Register in pytest.ini. Custom markers supported.

22. What is doctest integration?

Advanced

Answer: Pytest can run doctests with --doctest-modules

pytest --doctest-modules runs doctests. --doctest-glob="*.rst" for rst files. Integrates doctests into test suite. Useful for documentation testing.

23. How do you create custom markers?

Intermediate

Answer: Register in pytest.ini or pyproject.toml

Register markers in pytest.ini: [pytest] markers = slow: marks slow tests. Or pyproject.toml. Use @pytest.mark.slow. Run with pytest -m slow.

24. What is assert in Pytest?

Beginner

Answer: Python assert statement for test verification

Pytest uses Python's assert statement. assert expression shows detailed failure info. Pytest rewrites assert for better error messages. Simple and powerful.

Ready to test yourself?

The full Pytest bank has 90 questions across 3 difficulty levels — timed, shuffled, and scored.

Take the Pytest quiz