← Quiz: Unit Testing and TDD: all 10 questions
Quiz: Unit Testing and TDD · Question 2 of 10
What does this print? (code)
What does this print?
import io
import unittest
def square(x):
return x * x
class T(unittest.TestCase):
def test_a(self):
self.assertEqual(square(3), 9)
def test_b(self):
self.assertEqual(square(-2), -4)
def test_c(self):
self.assertEqual(square(0), 0)
r = unittest.TextTestRunner(stream=io.StringIO()).run(unittest.TestLoader().loadTestsFromTestCase(T))
print(r.testsRun, len(r.failures))
- A.3 0
- B.2 1
- C.3 1
- D.1 1
Answer & explanation
Correct answer: 3 1
All three tests run; test_b fails because (-2) × (-2) is 4, not -4.
Can you get the rest right without peeking?
Take the timed Quiz: Unit Testing and TDD quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →