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.