Unit Testing

Software Design
process

Also known as: unit tests, automated unit tests

Grade 9-12

View on concept map

Unit testing is the practice of testing the smallest useful parts of a program, such as a single function or module, in isolation. Unit testing supports maintenance, debugging, and teamwork.

Definition

Unit testing is the practice of testing the smallest useful parts of a program, such as a single function or module, in isolation. A unit test gives known input, checks the output, and helps confirm that the unit still behaves correctly after changes.

๐Ÿ’ก Intuition

Instead of testing the whole machine at once, test each small part separately so failures are easier to find.

๐ŸŽฏ Core Idea

Small automatic checks make code safer to change.

Example

A unit test for `isEven(4)` should expect `true`, while a test for `isEven(5)` should expect `false`.

Formula

\text{assert}(f(x) = y)

๐ŸŒŸ Why It Matters

Unit testing supports maintenance, debugging, and teamwork. It catches regressions early and gives students a disciplined way to confirm that code still works after edits.

๐Ÿ’ญ Hint When Stuck

Pick one function, choose one clear input, write the expected output first, and then run the function to compare the actual result. Add edge cases after the basic test passes.

Formal View

A unit test evaluates a single unit under controlled conditions and checks whether observed behavior matches a specified assertion.

๐Ÿšง Common Stuck Point

A unit test should focus on one behavior at a time. If it depends on half the program, it is no longer a small isolated test.

โš ๏ธ Common Mistakes

  • Writing tests that depend on external state instead of isolating the unit
  • Combining many behaviors into one large test so failures are hard to diagnose
  • Updating production code without rerunning the unit tests

Common Mistakes Guides

Frequently Asked Questions

What is Unit Testing in CS Thinking?

Unit testing is the practice of testing the smallest useful parts of a program, such as a single function or module, in isolation. A unit test gives known input, checks the output, and helps confirm that the unit still behaves correctly after changes.

What is the Unit Testing formula?

\text{assert}(f(x) = y)

When do you use Unit Testing?

Pick one function, choose one clear input, write the expected output first, and then run the function to compare the actual result. Add edge cases after the basic test passes.

How Unit Testing Connects to Other Ideas

To understand unit testing, you should first be comfortable with testing, function and edge cases. Once you have a solid grasp of unit testing, you can move on to version control.