7 ms·
I had a unit tests for a Java LocalDate that was being increased by one year. It checked whether the month and day were the same, and failed because the current
by bauble 3y ago
I had a unit tests for a Java LocalDate that was being increased by one year. It checked whether the month and day were the same, and failed because the current day (29) didn't equal the year-hence one (28). I changed it to check that the difference between the days was less than two.
- nathanwh 3y agoPreviously your unit test was broken one day every four ish years. Now it's broken every day :)
- bspammer 3y agoA unit test shouldn’t even be depending on the current date in the first place
- dimaaan 3y agoRight, fixed such test today
- yen223 3y agoThis is a hard-earned lesson, but it's true. Instead of doing def do_something_with_date(): now = datetime.now() return now - timedelta(days=2) you should do def do_something_with_date(now): return now - timedelta(days=2) and explicitly pass in edge-case dates into the `now` param in your unit tests. Alternatively, if you're using Python, use the freezegun library to fix the current time in tests: https://github.com/spulec/freezegun https://github.com/spulec/freezegun