Public course previewThis is not your assigned passport. No identity, answers, or completion progress are stored here.How to start your passport
Guided lesson
02 / 02About 35 minuteschanges practice files

Make And Verify A Reproducible Python Change

Expose a small calculation bug with a test, correct it, and inspect the two-file change.

Where you will workOn the laptop or desktop in front of you
  1. 1Understand
  2. 2Try it yourself
  3. 3Check and submit

Before you start#

A reproducible change includes the code, dependencies and command another person needs to repeat the check. A test is code that compares an observed result with an expected one. A Git diff shows which lines changed.

The fictional function calculates total memory from CPU count and memory per CPU. The existing test uses one CPU. It passes even though the function is incomplete: a green test only covers the case it ran.

Reading the function and test
def add_label_pages(content_pages, label_pages):
    return content_pages + label_pages

def defines a function; the names in parentheses are its inputs. return gives the result. add_label_pages(8, 2) returns 10. In the memory function, * multiplies and + adds; the same input-and-result idea applies.

self.assertEqual(add_label_pages(8, 2), 10)

The first part calls the function; the second is the expected result. assertEqual fails when they differ. This example only explains syntax: use the existing memory test as the template in your file. Keep its self and indentation, and start the new test's name with test_.

Read the function before editing. Why can its one-CPU example pass while a multi-CPU example fails? Choose a new input, calculate its expected total, then write a test before fixing the function. The check accepts equivalent integer arithmetic, rather than one supplied spelling.

How to read the intended failure

The assertion calls the function with four CPUs and 3 GiB per CPU and compares its result with 12. Before the fix it observes 3. That mismatch is the bug this exercise targets. An import, syntax or indentation error must be repaired before it can demonstrate the same behavior failure.

Changing the source before confirming that the regression test exposes the bug. Keep the test and its expected value; do not weaken it to get green output.

Start the practice

Practice

Try it in your practice folder

Follow in order

Steps

A test must fail for the missing behavior before the implementation is changed, then pass after the correction.

Optional: using the terminal

A command is a line of text that tells a computer to do one task. You paste it into a terminal, the text application used for commands. A shell is the part inside that application that reads the command. PowerShell, zsh, and Bash are shells. Open only the application named for your computer below; it starts its configured shell. Follow the shell named below. You do not need to install a separate Bash or zsh application.

Your computer or local means the laptop or desktop in front of you. Remote means another computer reached through the network. A command affects whichever computer the step names, so check that label before pasting.

  • Windows: press the Windows key, type PowerShell, and open the result named PowerShell. Do not use Command Prompt, Git Bash, or WSL for a block labelled PowerShell.
  • macOS: press Command+Space, type Terminal, and press Return. Terminal normally starts the zsh shell. If it uses another shell, run zsh for this session.
  • Linux: open the app named Terminal from the applications menu. It normally starts the Bash shell. If it uses another shell, run bash for this session.

The prompt is the text beside the blinking cursor. For each card, check the named computer, choose Copy, paste only the command after the prompt, press Enter once, and wait for the prompt to return.

If you have never done this before, first run the harmless terminal-ready practice on the start page. It lets you practise copying, pasting, pressing Enter, and reading output before a setup command.

Practise one harmless command firstHow to open a terminal and run a command
  1. 01

    Open the Python practice project

    On your computer

    Press Prepare practice folder in this step and run the displayed enter-folder command. Then activate ./.venv and move to workspace/python_project with the command below.

    Practice folder

    The launcher creates or reuses the exercise folder for this course. It does not change another folder.

    Open this lesson from your local Passport to prepare the folder.

    RunPowerShell on Windows
    conda activate ./.venv
    cd workspace/python_project
    RunTerminal on macOS (zsh)
    conda activate ./.venv
    cd workspace/python_project
    RunTerminal on Linux (Bash)
    conda activate ./.venv
    cd workspace/python_project

    Look forThe terminal is in workspace/python_project and uses the project environment.

    If this differsReturn to the environment mission; do not use a different Python.

    Explanation and help
    What this means
    Run the unchanged baseline.
  2. 02

    Run the baseline test

    On your computer

    On your first pass, run the complete declared test command before editing. If these files already contain your work, resume from your last step; do not delete a test or reset the function to repeat the lesson.

    RunPowerShell on Windows
    python -m unittest discover -s tests -v
    RunTerminal on macOS (zsh)
    python -m unittest discover -s tests -v
    RunTerminal on Linux (Bash)
    python -m unittest discover -s tests -v

    Look forOn a first pass, one test runs and passes. A green result covers that input, not every CPU count. Resumed work may already contain the second test.

    If this differsStop and repair the environment or baseline before changing code.

    Explanation and help
    What this means
    Read the source and existing test.
  3. 03

    Explain the missing behavior

    On your computer

    Read passport_example.py and tests/test_passport_example.py. Explain why the one-CPU test passes, then predict the result for four CPUs at 3 GiB per CPU before opening the example. If the syntax is unfamiliar, use Reading the function and test on this page; the optional link gives another function example. You do not need the rest of the Python course.

    Look forYou can compare the function inputs, its return value and the value the test expects.

    Explanation and help
    What this means
    With one CPU, returning memory per CPU happens to give the correct total. More than one CPU exposes the missing multiplication. An assertion compares observed behavior with the expected result.
    Safe recovery
    Re-read the function inputs and do not edit by trial and error.
  4. 04

    Add and run the regression test

    On your computer

    Keep test_one_cpu. Add another method inside TotalMemoryTests, at the same indentation. Choose more than one CPU and positive memory per CPU, using small integers up to 1,000; calculate the expected total yourself. Name the method test_ followed by a description. Save and run the suite before changing the function. An AssertionError comparing values is the intended failure; an import or indentation error is a different problem.

    RunPowerShell on Windows
    python -m unittest discover -s tests -v
    RunTerminal on macOS (zsh)
    python -m unittest discover -s tests -v
    RunTerminal on Linux (Bash)
    python -m unittest discover -s tests -v

    Look forAt least two tests run. The original test passes; your multi-CPU test fails because the old function returns memory per CPU, not the total.

    If this differsIf only one test runs, check the new name, indentation and saved file. If the new test passes or fails for another reason, correct the test before editing the implementation. Keep the independently calculated expected value; do not change it to match the bug.

    Explanation and help
    What this means
    Make the smallest implementation fix.
  5. 05

    Correct the implementation

    On your computer

    Change the return calculation to produce the total for every positive integer pair. Keep ValueError for either non-positive input. Ordinary multiplication, a temporary variable or equivalent integer arithmetic with +, - and * are accepted. Keep this small exercise free of extra imports, loops or helper calls. Optional tests using with self.assertRaises(ValueError) are accepted.

    Look forThe function returns the total while invalid non-positive inputs still raise ValueError.

    Explanation and help
    What this means
    Run the complete suite again.
    Safe recovery
    Revert only the mistaken edit in your editor and return to the failing test.
  6. 06

    Run the complete tests

    On your computer

    Run the same declared command in the same activated environment.

    RunPowerShell on Windows
    python -m unittest discover -s tests -v
    RunTerminal on macOS (zsh)
    python -m unittest discover -s tests -v
    RunTerminal on Linux (Bash)
    python -m unittest discover -s tests -v

    Look forThe original and your new test run and pass. Any invalid-input tests also pass. Keep the independently calculated expected result.

    If this differsRead the first failing traceback and fix its cause; do not delete or weaken a test.

    Explanation and help
    What this means
    Review repository state and diff.
  7. 07

    Review the reproducible change

    On your computer

    Stay in workspace/python_project. Review the changed files in this folder. Git diff shows line-by-line edits; . means the current folder. Check that source and tests changed, with no environment, cache or whitespace errors.

    RunPowerShell on Windows
    git status --short
    git diff --check
    git diff -- .
    RunTerminal on macOS (zsh)
    git status --short
    git diff --check
    git diff -- .
    RunTerminal on Linux (Bash)
    git status --short
    git diff --check
    git diff -- .

    Look forThe diff contains the intended source and regression test only.

    If this differsRemove generated files from the change and review again.

    Explanation and help
    What this means
    Run Check my work.
Try a different bug (optional)

Read this example without changing your practice files. It does not affect completion. You can write your prediction and test on paper; no extra folder, installation or submission is needed.

A box contains several identical items. The box is weighed once, but this fictional function has a bug:

def packed_weight_g(item_count, item_weight_g, box_weight_g):
    return item_count * (item_weight_g + box_weight_g)

A one-item test passes. For four items of 50 g in a 30 g box, what weight should a new test expect, and what will this function return? Write the assertion you would add before opening the explanation.

Hint

Which weight belongs to each item, and which belongs to the whole box?

Compare your test and correction
self.assertEqual(packed_weight_g(4, 50, 30), 230)

The buggy function returns 320: it counts the box weight four times. The expected 230 is four item weights plus one box weight. Keep that expected value even when the test fails. The correction is:

return item_count * item_weight_g + box_weight_g

Now consider zero items in a 20 g box. Choose any non-negative item weight. What should a test expect? This new case also exposes the original bug.

Practise

Two practice questions. They do not affect your progress.

01The test for one CPU passes. What does that establish?
See an explanation

A passing test supports the case it exercises. With one CPU, the incomplete return accidentally matches the total. The multi-CPU regression distinguishes the missing behavior.

02Before the fix, the new test reports ModuleNotFoundError rather than comparing 3 and 12. What next?
See an explanation

A failed import means the test has not reached the behavior comparison. Keep the implementation unchanged until the intended regression actually runs and fails.

Finish the lesson

Questions to submit

Answer these 2 questions, then confirm the work you checked.

To pass: 80% and every safety question. Completion appears after the automatic GitHub check passes.

01When may you record that tests passed?
02What belongs in the commit?Safety-critical
Before submitting

The launcher will copy only this lesson's declared fictional practice files into the public submission. Review those files as well as these answers.

Open my real passport first

You can check sample answers here. Open your local Passport to check your practice files and submit the lesson.

Preview onlyYou can try the questions here. This page does not store or submit your answers.
Need help?Open recovery steps and AI-review guidance

If Blocked#

Return to the last passing baseline, inspect the first failing assertion, and reduce the problem. Do not delete tests, broaden tolerances, or install random packages merely to make the check green.

Useful references:

Understand Before Accepting AI Output#

If an agent explains the failure, independently inspect the affected source and test. Do not accept a test that only repeats the implementation or a claim that was not personally run.