Skills101
← All practice quizzes

Quiz: Testing Python Code

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

Stop checking your code by hand. Write automatic tests with assert, unittest, pytest and doctest, choose good test cases with boundary values, and see how a failing test points straight to a bug.

Log in to take the quiz

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

  1. Question 1

    What happens when assert x > 0, "x must be positive" runs with x = -2?

  2. Question 2

    Why should you not use assert to validate user input in a real program?

  3. Question 3

    In unittest, which methods of a TestCase class are run as tests?

  4. Question 4

    What is setUp used for?

  5. Question 5

    What does this print?

    import io
    import unittest
    
    class T(unittest.TestCase):
        def test_a(self):
            self.assertEqual(2 + 2, 4)
        def test_b(self):
            self.assertEqual(len("abc"), 4)
        def helper(self):
            self.assertTrue(False)
    
    r = unittest.TextTestRunner(stream=io.StringIO()).run(unittest.TestLoader().loadTestsFromTestCase(T))
    print(r.testsRun, len(r.failures))
    
🎯 Today’s placement challenge — 10 questions, no login. Can you score 10/10?Take today’s challenge →