Skip to main content
added 297 characters in body
Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

Lets assume you are testing a class "interval", representing intervals of natural numbers:

 var interval= new Interval(1,100); 

Now your first two tests are "green":

AssertIsTrue(interval.Contains(100)) AssertIsFalse(interval.Contains(101)) 

and you are confident the author made no "off-by-one-error".

Unfortunately, the implementation looks like this

Contains(x) { return (x>= beginRange && x == endRange); // above line has a typo, "==" should have been be "<=" } 

So you were better off to add another test at first hand,

 AssertIsTrue(interval.Contains(99)); 

which fails, revealing the bug missed by the first two tests.

Or to be more general: off-by-one errors do not always manifest themselves by mixing up "<" and "<=", there are lots of other possibilities to get such things wrong, and as a tester, you are better off not to make any assumptions about how the internal implementation of a function looks like.

Lets assume you are testing a class "interval", representing intervals of natural numbers:

 var interval= new Interval(1,100); 

Now your first two tests are "green":

AssertIsTrue(interval.Contains(100)) AssertIsFalse(interval.Contains(101)) 

and you are confident the author made no "off-by-one-error".

Unfortunately, the implementation looks like this

Contains(x) { return (x>= beginRange && x == endRange); // above line has a typo, "==" should have been be "<=" } 

So you were better off to add another test at first hand,

 AssertIsTrue(interval.Contains(99)); 

which fails, revealing the bug missed by the first two tests.

Lets assume you are testing a class "interval", representing intervals of natural numbers:

 var interval= new Interval(1,100); 

Now your first two tests are "green":

AssertIsTrue(interval.Contains(100)) AssertIsFalse(interval.Contains(101)) 

and you are confident the author made no "off-by-one-error".

Unfortunately, the implementation looks like this

Contains(x) { return (x>= beginRange && x == endRange); // above line has a typo, "==" should have been be "<=" } 

So you were better off to add another test at first hand,

 AssertIsTrue(interval.Contains(99)); 

which fails, revealing the bug missed by the first two tests.

Or to be more general: off-by-one errors do not always manifest themselves by mixing up "<" and "<=", there are lots of other possibilities to get such things wrong, and as a tester, you are better off not to make any assumptions about how the internal implementation of a function looks like.

Source Link
Doc Brown
  • 220.5k
  • 35
  • 410
  • 625

Lets assume you are testing a class "interval", representing intervals of natural numbers:

 var interval= new Interval(1,100); 

Now your first two tests are "green":

AssertIsTrue(interval.Contains(100)) AssertIsFalse(interval.Contains(101)) 

and you are confident the author made no "off-by-one-error".

Unfortunately, the implementation looks like this

Contains(x) { return (x>= beginRange && x == endRange); // above line has a typo, "==" should have been be "<=" } 

So you were better off to add another test at first hand,

 AssertIsTrue(interval.Contains(99)); 

which fails, revealing the bug missed by the first two tests.