Bird
0
0

Given this pytest code, what will be the output when running test_example?

medium📝 Predict Output Q13 of 15
PyTest - Fixtures
Given this pytest code, what will be the output when running test_example?
import pytest

@pytest.fixture
def sample_data():
    return [1, 2, 3]

def test_example(sample_data):
    assert sum(sample_data) == 6
    print('Test passed')
ANo output because print is ignored
BTest fails due to wrong sum
CSyntaxError because fixture is missing
DTest passed printed and test passes
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture usage in test

    The fixture sample_data returns [1, 2, 3]. The test asserts sum is 6, which is correct.
  2. Step 2: Check test behavior

    Since assertion passes, test passes. Pytest captures stdout by default and does not show print output for passing tests (use -s to show). Thus, no 'Test passed' is printed.
  3. Final Answer:

    No output because print is ignored -> Option A
  4. Quick Check:

    Fixture provides data, assertion correct, test passes [OK]
Quick Trick: pytest captures stdout by default for passing tests [OK]
Common Mistakes:
MISTAKES
  • Thinking print output is shown by default
  • Thinking fixture is missing causing error
  • Believing sum is incorrect

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes