Unit testing software can be difficult, especially when you are considering what to test. You should test the happy path that you expect most of your code to adhere to, but what else? I use a concept I call Fencepost Testing to help me determine if I have sufficient test coverage. The concept is simple, consider your code and identify the boundary conditions it should adhere to. If what you are testing has one boundary or a “fencepost” then test that post. If you find multiple boundaries then you have multiple “fenceposts”, you should test each post and a wildcard in between. The “fenceposts” being the make it or break it components of the subject you are testing.
So, for some code that checks if a date is in the future I would breakup my tests as such:
- Some arbitrary date in the past
- Yesterday (Fencepost - Immediate boundary)
- Today (Fencepost)
- Tomorrow (Fencepost - Immediate boundary)
- Some arbitrary date in the future
Some would argue that the arbitrary dates would not matter, but they serve as sanity checks to make sure I’m noti, for example, using the month value alone. The arbitrary dates could be random, since they should never fail. If they did, well then you’ve caught a bug.
Consider the regex
^\$\d+$ which simply tests is a string is a dollar amount. A slightly more complicated item to test, with an infinite number of posibilities that would and would not match. But, there are some fenceposts:
- Must start with $
- Must have a digit following the $
- Can have many digits there after
- Last character must be a digit
So my test cases would be:
- ’ $1’ (There is a whitespace character at the beginning)
- ‘a$1’ (Non-whitespace character before $)
- ’$’ (Nothing following the $)
- ‘$a’ (Non-whitespace non-digit character after $)
- ’$ ’ (Whitespace character after $)
- ‘$1’ (Simple one digit happy path)
- ‘$1234567890’ (Simple multi-digit happy path)
- ‘$1 ’ (One digit with trailing whitespace)
- ‘$1a’ (One digit with trailing non-digit character)
- ‘$1a1’ (Digits with intermittent non-digit character)
- ‘$1,000’ (Digits with comma separated non-digit character)
In essence this should cover most of the cases that could be applied to that regex. If you do find a bug that passes this regex enexpectedly then add a new test case.
Testing the posts alone, or solely the happy path is not different than building fenceposts with no fence between them.