{"id":941,"date":"2019-11-27T13:56:18","date_gmt":"2019-11-27T13:56:18","guid":{"rendered":"http:\/\/buklijas.info\/blog\/?p=941"},"modified":"2019-12-29T18:37:15","modified_gmt":"2019-12-29T18:37:15","slug":"941","status":"publish","type":"post","link":"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/","title":{"rendered":"Mock time in Python unit tests"},"content":{"rendered":"

Published on:<\/strong> 27.11.2019<\/p>\n

From my point of view, unit tests are useful, especially for refactoring functions that have clearly defined inputs and outputs.<\/p>\n

One thing that is impossible to test is anything regarding time\/date code because whenever you run it is a different time \ud83d\ude42 .<\/p>\n

For example, if you have a function that calculates the number of days between two dates, the only way to test is to somehow mock<\/a> current time\/date.<\/p>\n

in Python, there is FreezeGun<\/a>.<\/p>\n

@freeze_time(\"2019-09-11\")\ndef test_wrong_dates_one(test_input, expected_output):\n     assert calculate_date(test_input) == expected_output\n<\/pre>\n

It is implemented as a decorator, which is handy and readable in code.<\/p>\n

So far I have not found any problem with FreezeGun.<\/p>\n","protected":false},"excerpt":{"rendered":"

Published on: 27.11.2019 From my point of view, unit tests are useful, especially for refactoring functions that have clearly defined inputs and outputs. One thing that is impossible to test is anything regarding time\/date code because whenever you run it is a different time \ud83d\ude42 . For example, if you have a function that calculates […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false},"categories":[27],"tags":[64,4,65],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"yoast_head":"\nMock time in Python unit tests - Sasa Buklijas<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Mock time in Python unit tests - Sasa Buklijas\" \/>\n<meta property=\"og:description\" content=\"Published on: 27.11.2019 From my point of view, unit tests are useful, especially for refactoring functions that have clearly defined inputs and outputs. One thing that is impossible to test is anything regarding time\/date code because whenever you run it is a different time \ud83d\ude42 . For example, if you have a function that calculates […]\" \/>\n<meta property=\"og:url\" content=\"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/\" \/>\n<meta property=\"og:site_name\" content=\"Sasa Buklijas\" \/>\n<meta property=\"article:published_time\" content=\"2019-11-27T13:56:18+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-12-29T18:37:15+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\">\n\t<meta name=\"twitter:data1\" content=\"Sasa Buklijas\">\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data2\" content=\"0 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"http:\/\/buklijas.info\/blog\/#website\",\"url\":\"http:\/\/buklijas.info\/blog\/\",\"name\":\"Sasa Buklijas\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"http:\/\/buklijas.info\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/#webpage\",\"url\":\"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/\",\"name\":\"Mock time in Python unit tests - Sasa Buklijas\",\"isPartOf\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#website\"},\"datePublished\":\"2019-11-27T13:56:18+00:00\",\"dateModified\":\"2019-12-29T18:37:15+00:00\",\"author\":{\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"http:\/\/buklijas.info\/blog\/2019\/11\/27\/941\/\"]}]},{\"@type\":\"Person\",\"@id\":\"http:\/\/buklijas.info\/blog\/#\/schema\/person\/780025d597f1c5df3cc156eaffc8c561\",\"name\":\"Sasa Buklijas\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"http:\/\/buklijas.info\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/0.gravatar.com\/avatar\/9f6f7de5a4882517ca0e4a8ebd607925?s=96&d=mm&r=g\",\"caption\":\"Sasa Buklijas\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/s5YHGV-941","_links":{"self":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/941"}],"collection":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/comments?post=941"}],"version-history":[{"count":14,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/941\/revisions"}],"predecessor-version":[{"id":959,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/posts\/941\/revisions\/959"}],"wp:attachment":[{"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/media?parent=941"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/categories?post=941"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/buklijas.info\/blog\/wp-json\/wp\/v2\/tags?post=941"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}