PyTest interview questions — Medium
252 medium-level PyTest questions from our Automation Testing bank, each with the correct answer and an explanation of why it is correct.
What this covers
PyTest appears throughout Automation Testing interviews. At medium level, interviewers are typically checking that you have used it in practice: what you configured, what broke, and how you knew it worked. Work through these, then explain your answer out loud; the second part is what interviews actually test.
20 example questions
1. "CLI option stopping the run after the first failure" — in PyTest, this describes which of the following?
- A. Tmp_path.
- B. Capsys.
- C. Pytest.ini / pyproject.toml.
- D. --maxfail=1.correct
Why: --maxfail=1: CLI option stopping the run after the first failure.
2. "Built-in fixture for safely patching attributes and env vars" — in PyTest, this describes which of the following?
- A. --maxfail=1.
- B. @pytest.fixture(autouse=True).
- C. Monkeypatch.correct
- D. Pytest -m smoke.
Why: monkeypatch: Built-in fixture for safely patching attributes and env vars.
3. "Fixture applied automatically without being requested" — in PyTest, this describes which of the following?
- A. Pytest.ini / pyproject.toml.
- B. Pytest -m smoke.
- C. Tmp_path.
- D. @pytest.fixture(autouse=True).correct
Why: @pytest.fixture(autouse=True): Fixture applied automatically without being requested.
4. Which decorator runs a pytest test multiple times with different inputs?
- A. @pytest.fixture.
- B. @pytest.mark.parametrize.correct
- C. @pytest.mark.skip.
- D. @pytest.mark.usefixtures.
Why: @pytest.mark.parametrize('arg', [..]) runs the test once per value, and pytest's assert rewriting shows rich failure details.
5. "Runs only tests marked with the smoke marker" — in PyTest, this describes which of the following?
- A. Capsys.
- B. Pytest.ini / pyproject.toml.
- C. Tmp_path.
- D. Pytest -m smoke.correct
Why: pytest -m smoke: Runs only tests marked with the smoke marker.
6. "Configuration files defining markers, paths, and options" — in PyTest, this describes which of the following?
- A. Tmp_path.
- B. Monkeypatch.
- C. Capsys.
- D. Pytest.ini / pyproject.toml.correct
Why: pytest.ini / pyproject.toml: Configuration files defining markers, paths, and options.
7. Identify the accurate statement about PyTest:
- A. Pytest ships with parallel execution enabled without any plugin.
- B. Pytest requires every test class to inherit from unittest.TestCase.
- C. Markers like @pytest.mark.skip and @pytest.mark.xfail control expected outcomes.correct
- D. Pytest cannot express an expected-to-fail test.
Why: xfail marks tests expected to fail without breaking the suite.
8. Which statement correctly describes PyTest?
- A. Assertions in pytest must use self.assertEqual style methods.
- B. Conftest.py shares fixtures and hooks across multiple test files without imports.correct
- C. Pytest requires every test class to inherit from unittest.TestCase.
- D. Pytest cannot express an expected-to-fail test.
Why: Fixtures in conftest.py are auto-discovered by directory scope.
9. "Fixture capturing stdout/stderr output" — in PyTest, this describes which of the following?
- A. Monkeypatch.
- B. Capsys.correct
- C. Pytest.ini / pyproject.toml.
- D. Tmp_path.
Why: capsys: Fixture capturing stdout/stderr output.
10. Which of the following statements about PyTest is TRUE?
- A. Assertions in pytest must use self.assertEqual style methods.
- B. Pytest ships with parallel execution enabled without any plugin.
- C. Fixtures must be explicitly imported into every test file that uses them.
- D. Fixture scope can be function, class, module, package, or session.correct
Why: Broader scopes reuse expensive resources like DB connections.
11. "Plugin generating an HTML test report" — in PyTest, this describes which of the following?
- A. Pytest -m smoke.
- B. Pytest-html.correct
- C. Tmp_path.
- D. Monkeypatch.
Why: pytest-html: Plugin generating an HTML test report.
12. Which decorator runs a pytest test with multiple inputs?
- A. @pytest.fixture.
- B. @pytest.mark.parametrize.correct
- C. @pytest.mark.skip.
- D. @pytest.mark.usefixtures.
Why: @pytest.mark.parametrize runs the test once per value.
13. Which PyTest concept matches this description: "Runs only tests marked with the smoke marker"?
- A. Pytest-html.
- B. Tmp_path.
- C. Monkeypatch.
- D. Pytest -m smoke.correct
Why: pytest -m smoke: Runs only tests marked with the smoke marker.
14. Which PyTest concept matches this description: "Built-in fixture for safely patching attributes and env vars"?
- A. Tmp_path.
- B. --maxfail=1.
- C. Monkeypatch.correct
- D. @pytest.fixture(autouse=True).
Why: monkeypatch: Built-in fixture for safely patching attributes and env vars.
15. In PyTest, which of the following is described as: "Runs only tests marked with the smoke marker"?
- A. Pytest.ini / pyproject.toml.
- B. Capsys.
- C. Pytest -m smoke.correct
- D. Monkeypatch.
Why: pytest -m smoke: Runs only tests marked with the smoke marker.
16. In the context of PyTest, which of the following is a correct statement?
- A. Pytest requires every test class to inherit from unittest.TestCase.
- B. Fixtures must be explicitly imported into every test file that uses them.
- C. Pytest fixtures provide reusable setup via dependency injection into test function parameters.correct
- D. Pytest ships with parallel execution enabled without any plugin.
Why: A test requests a fixture simply by naming it as an argument.
17. Which PyTest concept matches this description: "CLI option stopping the run after the first failure"?
- A. --maxfail=1.correct
- B. Capsys.
- C. Pytest.ini / pyproject.toml.
- D. @pytest.fixture(autouse=True).
Why: --maxfail=1: CLI option stopping the run after the first failure.
18. Which PyTest concept matches this description: "Fixture applied automatically without being requested"?
- A. Capsys.
- B. Monkeypatch.
- C. @pytest.fixture(autouse=True).correct
- D. Pytest -m smoke.
Why: @pytest.fixture(autouse=True): Fixture applied automatically without being requested.
19. In PyTest, which of the following is described as: "Configuration files defining markers, paths, and options"?
- A. Pytest -m smoke.
- B. Tmp_path.
- C. Capsys.
- D. Pytest.ini / pyproject.toml.correct
Why: pytest.ini / pyproject.toml: Configuration files defining markers, paths, and options.
20. In PyTest, which of the following is described as: "Built-in fixture for safely patching attributes and env vars"?
- A. Monkeypatch.correct
- B. Capsys.
- C. Pytest -m smoke.
- D. Tmp_path.
Why: monkeypatch: Built-in fixture for safely patching attributes and env vars.