Rullst AcademyRullst Academy/Lesson
Python: from first line to project · Lesson 11 / 12

Tests for better-founded confidence

Your program got one example right. Does it respect the boundary too? A test records an expectation to repeat after each change.

Learning paths12 min

Let's discover together

  1. 1

    unittest is part of Python. A class derived from unittest.TestCase groups tests; methods starting with test_ describe cases. You do not need to master classes yet: use the shown pattern.

  2. 2

    self.assertEqual(result, expected) compares values and records a failure if they differ. We test a typical case and equality at the boundary.

  3. 3

    The demonstrations run the suite in memory and show test count and success. Version 2 adds + 1 to the rule: the same tests still run but no longer pass.

  4. 4

    On a computer, python -m unittest lesson.py -v (python3 on macOS/Ubuntu) shows details. Tests support confidence in covered cases; they do not prove every defect is absent.

✦
YOUR MISSION

Write unittest tests and interpret failure without changing expectations to hide a defect.

Now it's your turn

Discovery workshop

Read, predict, check

Compare the two versions. Show result reveals a prepared output: this panel does not run Python. Then practise with the activity file.
Mini experiment

Learning simulation: compare cards and check your predictions. The browser is not compiling Rust or starting a server.

import unittest

def remaining(limit, used):
    return limit - used

class TimeTests(unittest.TestCase):
    def test_regular(self):
        self.assertEqual(remaining(30, 10), 20)

    def test_exact_limit(self):
        self.assertEqual(remaining(30, 30), 0)

if __name__ == "__main__":
    suite = unittest.defaultTestLoader.loadTestsFromTestCase(TimeTests)
    result = unittest.TestResult()
    suite.run(result)
    print(result.testsRun)
    print(result.wasSuccessful())
Build your answers and check when you are ready.
A moment to understand

After a change, a test representing the correct rule fails. What next?

I need a hint

A useful test makes a mismatch visible.

Loading your journey…

Take it with you

Add a test for remaining(30, 0), expecting 30. Run, introduce a small defect, observe the failure and fix the code. Explain one case your tests still do not cover.

PYTHON · .py

Your Python workbench

Here you read and predict prepared outputs. To run the actual code, download the activity file, edit it on your computer and use Python. Rullst Academy grades lesson answers; it does not verify local execution.

Save and run

Extract the activity ZIP into its own folder. Open a terminal there and use your system’s command. If downloading your edited file separately, use its actual name in the command. Save before running again.

Windows
python lesson.py
macOS / Ubuntu
python3 lesson.py
My files and GitHub

The editor keeps a local draft and can send a .py file to GitHub with your confirmation. Download your edited code to preserve it. The ZIP is starter material; for the final project, also publish data and test files following PUBLISH.md.

At your own pace

My files and GitHub

Every lesson can become your own record: code, notes or a practice plan. Saving a file to GitHub is optional and does not change your XP.

Student guide

The editor and downloads open when this lesson is available to your account. Check access below or return to the track map.

Checking your access to this lesson…

Rullstpetbot

AI can make mistakes. Check the lesson material.