Skills101
← All practice quizzes

Quiz: Unit Testing and TDD

5 questions · new set on every retakeNo time limitPass: 60%+10 XP on first pass

How developers test their own code automatically. Writing unit tests with Python's unittest, the Arrange-Act-Assert shape, mocks and stubs, the test pyramid, code coverage, and test-driven development (red, green, refactor) on a real example.

Log in to take the quiz

Free account · see your score, answers and explanations, earn XP

  1. Question 1

    What are the three parts of a well-structured unit test?

  2. Question 2

    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))
    
  3. Question 3

    In TDD, what must happen in the "red" step?

  4. Question 4

    What does this print?

    from unittest.mock import Mock
    
    mailer = Mock()
    for marks in [35, 72, 28, 90]:
        if marks < 40:
            mailer.send('parent', marks)
    print(mailer.send.call_count, mailer.send.call_args.args)
    
  5. Question 5

    What is the difference between a stub and a mock?

🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →