I have a small method which return true or false if a timestamp is a weekend day i.e. saturday or sunday.
Now i am very new to unit testing and i am trying to write a unit test for this.
In my test case, how would i proceed:
Here is my inital thinking.
1. Pick any 1 week from the past and then...
1.1. Get a timestamp for all 5 week days (mon through fri) and pass each timestamp to the function being tested. If they all return false then proceed...
1.2 Get the timestamp for both weekend days and pass each to function being tested. If they both return true then we know the function is correct.
or
2 Simply pick 1 weekday from the past and 1 weekend day from the past and test with only those 2 dates
Am i correct in either of these approaches or is there a better way to test this?
If you put several checks into a single test, you will have the problem that you won’t know what some of the checks might return when one of the first ones fails. Say the method fails for day #3. Would it work for day #4? This information could be very useful when trying to find the bug.
My approach is to test all values at once. It works like this:
This way, you can see with a single glance for which dates the method fails.
The other option is to write 8 tests and have each test check a single date.
Tip: Tests like this tend to fail when timezones are introduced. Create more tests which use timestamps that are close to midnight and play with the timezome. Is the result still correct?