Bird
0
0

Given the following pytest code, what will be the output when running the test?

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

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

def test_sum(sample_data):
    assert sum(sample_data) == 6
    print('Test passed')
ANo output printed, test passes silently
BTest fails with assertion error
CSyntaxError due to fixture usage
DTest passed printed and test passes
Step-by-Step Solution
Solution:
  1. Step 1: Understand fixture usage

    The fixture sample_data returns a list [1, 2, 3]. The test receives this list as argument.
  2. Step 2: Check assertion and print

    The sum of [1, 2, 3] is 6, so the assertion passes. The print statement runs and outputs 'Test passed'.
  3. Final Answer:

    Test passed printed and test passes -> Option D
  4. Quick Check:

    Fixture provides data, assertion true, print runs [OK]
Quick Trick: Fixture returns data; test uses it and prints on success [OK]
Common Mistakes:
MISTAKES
  • Assuming fixture causes syntax error
  • Thinking print won't show during test run
  • Believing assertion fails with correct sum

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PyTest Quizzes