Test All The Things


Teaching: 10 min
Exercises: 15 min
  • How can I tell if my software is good enough to release?

  • How can figure out what my code is supposed to do?

  • How can I stay focused on building what I actually need to?

  • Explain why scientists should think about testing in terms of tolerances.

  • Write unit tests to define what what a simple function is supposed to do.

What Are Your Tolerances?

  1. What will you measure to determine whether your software is correct enough?
  2. What tolerances will you accept on answers?
  3. Why?

Pick a Unit Testing Framework

  1. If your project already uses a unit testing framework, explain which one and why you chose it.
  2. If your project does not already use a unit testing framework, find one and create one test using it.

First, the Tests

  1. Using assert statements, write half a dozen tests for each of the following functions.
  2. Compare your tests to those written by your neighbors. What errors did they test for that you didn’t? What would your tests catch that they missed?
  3. Where did you interpret requirements differently? I.e., where would a function pass one of your neighbors’ tests but fail one of yours or vice versa?
Non-Decreasing Sub-Lists
Given a list of numbers, return a list of the sums of each non-decreasing sub-list. For example, if the input is [1, 2, 3, 3, 1, 5, 6, 3, 1, 2, 3], the output should be [9, 12, 3, 6].
Rectangle Overlay
Given two rectangles, each defined by the four values [x0, y0, x1, y1], return the rectangle representing their overlap. Assume all coordinates are integer. For example, if the inputs are [0, 0, 2, 2] and [1, 1, 5, 3], the output should be [1, 1, 2, 2].

Key Points

  • Write tests to define explicit tolerances.

  • Use a unit testing framework to write and run tests.

  • Isolate tests.