top | item 39556636

(no title)

nathanwh | 2 years ago

Previously your unit test was broken one day every four ish years. Now it's broken every day :)

discuss

order

bspammer|2 years ago

A unit test shouldn’t even be depending on the current date in the first place

yen223|2 years ago

This 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

dimaaan|2 years ago

Right, fixed such test today