Python unittest
You add 2 and 3. You want to check that the result is 5. You will run that check. The last line on the screen will be OK.
import unittest
def add(left, right):
return left + right
class Check(unittest.TestCase):
def test_sum(self):
self.assertEqual(add(2, 3), 5)
unittest.main()
The last line on the screen is OK. The first line is a dot. That dot is this one check. A later line starts with Ran 1 test. add(2, 3) returns 5. The line self.assertEqual(add(2, 3), 5) checks that 5 against the 5 you wanted. They match, so the last line is OK. The name test_sum starts with test_, so this check runs. The program is done.
If that check compared add(2, 3) with 9, the last line would be FAILED (failures=1), because 5 is not 9. That program is not this one. This program compares add(2, 3) with 5, so the last line is OK.
📊 The check and the last line
| The check | The last line |
|---|---|
| add(2, 3) and 5 | OK |
You can now check that a result matches the result you wanted. The last line on this screen is OK. The next page reads one word you put after the program name.
The last line is OK
The last line is OK. The check compared add(2, 3) with 5. The next page reads one word you put after the program name.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top