Bird
0
0

What will be the output when running this Python unittest code?

medium📝 Predict Output Q5 of 15
Testing Fundamentals - Testing Types and Levels
What will be the output when running this Python unittest code?
import unittest

def is_even(n):
    return n % 2 == 0

class TestIsEven(unittest.TestCase):
    def test_even(self):
        self.assertTrue(is_even(4))
    def test_odd(self):
        self.assertFalse(is_even(5))

if __name__ == '__main__':
    unittest.main()
ABoth tests fail
BBoth tests pass
Ctest_even passes, test_odd fails
Dtest_even fails, test_odd passes
Step-by-Step Solution
Solution:
  1. Step 1: Understand the is_even function

    is_even returns True if number is divisible by 2, else False.
  2. Step 2: Check each test case

    test_even asserts is_even(4) is True (4 is even). test_odd asserts is_even(5) is False (5 is odd). Both assertions are correct.
  3. Final Answer:

    Both tests pass -> Option B
  4. Quick Check:

    Correct assertions = Both tests pass [OK]
Quick Trick: True for even, False for odd tests pass [OK]
Common Mistakes:
  • Mixing up True and False assertions
  • Assuming odd number test passes incorrectly
  • Ignoring function logic

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes