Practice Unit Testing in CS Thinking

Use these practice problems to test your method after reviewing the concept explanation and worked examples.

Quick Recap

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.

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

Showing a random 20 of 50 problems.

Example 1

easy
Which is NOT a typical input class to cover when unit-testing a function: (a) typical/happy-path, (b) boundary, (c) error, (d) marketing copy?

Example 2

medium
`round half up`: `roundHalf(2.5)` should give 3. A test asserts `roundHalf(2.5) == 2`. The code returns 3. Did the test catch a bug, or is the test wrong?

Example 3

easy
Which test best isolates a bug: A) one big test exercising 10 behaviors, or B) ten small tests, one per behavior?

Example 4

medium
Tests should be FAST so they run on every change. A test that sleeps 30 seconds for a timer violates this. What testing fix keeps it fast?

Example 5

hard
A test mocks a database to return a fixed user. The test passes, but production breaks when the real DB returns null. What is the lesson, and what additional test category is needed?

Example 6

challenge
Following TDD, you write `assert isPrime(2)==True`, `assert isPrime(4)==False`, `assert isPrime(1)==False`. Describe the red-green-refactor cycle as you implement `isPrime`.

Example 7

hard
You write a property-based test asserting that for any list L, `reverse(reverse(L)) == L`. Name two specific edge cases the property already covers without you enumerating them.

Example 8

easy
After fixing a bug, what should a developer add to the test suite to prevent recurrence?

Example 9

easy
Tests run as part of a developer's commit pipeline before merging are called ____ tests.

Example 10

hard
A test that pins the current (possibly wrong) output of a function so you can refactor safely is called a ____ test.

Example 11

easy
A unit test for `multiply(4, 5)` should assert the result equals what value?

Example 12

medium
A test that creates a temporary file but never deletes it eventually fills the disk in CI. What test-suite property is missing?

Example 13

hard
Two passing tests assert `divide(6,2)==3` and `divide(9,3)==3`. A bug makes `divide` return `3` for any input. Which single new test would catch this bug most cheaply?

Example 14

medium
A test asserts `sort([3,1,2]) == [1,2,3]` but also that the function printed a log line. Why does asserting on the log line weaken the unit test?

Example 15

easy
True or False: a passing unit test proves the function is bug-free for every possible input.

Example 16

easy
A test passes whether or not the code is correct because it asserts nothing meaningful. What is this test called?

Example 17

hard
Mutation testing changes one operator in your code and reruns the tests. If all tests still pass, what does that suggest about the test suite?

Example 18

challenge
A team has 100% line coverage yet a boundary bug ships. Explain how full line coverage can still miss the bug, and what coverage idea would have caught it.

Example 19

medium
Design 3 unit tests for `reverse(string)`. List input -> expected pairs covering normal, empty, and palindrome cases.

Example 20

medium
`fizzbuzz(n)` returns 'Fizz' if divisible by 3, 'Buzz' if by 5, 'FizzBuzz' if by both, else the number as string. Give expected outputs for n=3,5,15,7n=3,5,15,7.