0

So one of the goals of unit testing is to make sure that future changes/refactors won't break existing functionality. Suppose we have the following method:

public bool LessThanFive(double a) {
  return a < 5;
}

One way to uni test this is as follow:

public bool LessThanFiveTests_True() {
  const double a = 4;
  Assert.IsTrue(LessThanFive(a))
}

The problem with this unit test is that if, later on, someone changes the < into <= in LessThanFive method, the test will pass.

What about if we have DateTime instead of double?

havij
  • 1,030
  • 14
  • 29
  • Regarding the point with `DateTime`: Could you please explain how the code with `DateTime` would look that you would like to tests? – Dirk Herrmann Feb 16 '19 at 10:12

1 Answers1

1

Your assumption seems to be that you write one test, and that test catches all possible bugs. The opposite is closer to the truth: For each potential bug, you need one test to catch it. In practice, certainly, many tests will catch a number of potential bugs, but I exaggerated a bit to convey the idea.

So, to catch the potential bug that the < is turned into a <=, you need a second test case that tries the function with 5. This is, btw. a so-called boundary test, and boundary testing is one well-known method to derive a set of useful test cases.

There are many more test design techniques, like, equivalence class partitioning, classification trees, coverage based testing etc. The underlying goal of these methods is, to guide you in developing a test suite where ideally for every potential bug a corresponding test case exists that will detect that bug.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47
  • What about the test that tries to make sure any value less than 5 for the parameter results in true? More specifically, which value should be used for `a` in the test? What if the type is `DateTime`? – havij Feb 16 '19 at 05:11
  • For `a`, if you really want to test the boundaries exactly, you could use something like `nextDown(5.0)`, given that C# has that function. See: https://stackoverflow.com/questions/1245870/next-higher-lower-ieee-double-precision-number. – Dirk Herrmann Feb 16 '19 at 10:10