Travel to the past is probably impossible. But are there ways to time-travel into the future?
Travel to the past is probably impossible. But are there ways to time-travel into the future?
Time travel testing is the act of testing date and time sensitive functionality in order to validate business rules and logic that exists in a software stack. Other common names for this type of testing are
No matter the name, this type of testing is vital in discovering “date bugs” that exist throughout applications. These date bugs are platform and language independent and can exist in any application. Date bugs are most common in applications that rely heavily on date & time logic, such as financial, government, healthcare, energy, and insurance applications
How to test this code’s logic as it will execute in the future ?
def create_report(): now = datetime.now() if now.month == 12 and now.year == 2022: generate_report(now)
def create_report(): now = datetime.now() if now.month == 12 and now.year == 2022: generate_report(now)
1: Change System Time, and re-run the service
1: Change System Time, and re-run the service
2: Change the code logic, pass the future time
+ def create_report(now): - def create_report(): - now = datetime.now() if now.month == 12 and now.year == 2022: generate_report(now) + create_report(datetime.datetime.strptime('2022-12-01', '%Y-%m-%d'))
+ def create_report(now): - def create_report(): - now = datetime.now() if now.month == 12 and now.year == 2022: generate_report(now) + create_report(datetime.datetime.strptime('2022-12-01', '%Y-%m-%d'))
2: Change the code logic, pass the future time
The Pain points for developer and QA
Let your Python tests travel through time FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
from freezegun import freeze_time import datetime import unittest # Freeze time for a pytest style test: @freeze_time("2012-01-14") def test(): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) # Or a unittest TestCase - freezes for every test, and set up and tear down code @freeze_time("1955-11-12") class MyTests(unittest.TestCase): def test_the_class(self): assert datetime.datetime.now() == datetime.datetime(1955, 11, 12) @freeze_time("2012-01-14") class Tester(object): def test_the_class(self): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
from freezegun import freeze_time import datetime import unittest # Freeze time for a pytest style test: @freeze_time("2012-01-14") def test(): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) # Or a unittest TestCase - freezes for every test, and set up and tear down code @freeze_time("1955-11-12") class MyTests(unittest.TestCase): def test_the_class(self): assert datetime.datetime.now() == datetime.datetime(1955, 11, 12) @freeze_time("2012-01-14") class Tester(object): def test_the_class(self): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14)
Let your Python tests travel through time FreezeGun is a library that allows your Python tests to travel through time by mocking the datetime module.
Context manager
from freezegun import freeze_time def test(): assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) with freeze_time("2012-01-14"): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
from freezegun import freeze_time def test(): assert datetime.datetime.now() != datetime.datetime(2012, 1, 14) with freeze_time("2012-01-14"): assert datetime.datetime.now() == datetime.datetime(2012, 1, 14) assert datetime.datetime.now() != datetime.datetime(2012, 1, 14)
Example In Juno Payroll Testing
Run Celery task in brower with an specific Date
from tasks.create_report import create_report with freeze_time("2012-01-14"): create_report()
from tasks.create_report import create_report with freeze_time("2012-01-14"): create_report()
Example In Integration by playwright
from playwright.sync_api import Page, expect def test_send_command( page: Page, ): page.goto("https://juno-staging.joinhorizons.com") page.evaluate( "() => RunIn('2022-12-21','run_task',['create_employee_payroll_report_items'])" )
from playwright.sync_api import Page, expect def test_send_command( page: Page, ): page.goto("https://juno-staging.joinhorizons.com") page.evaluate( "() => RunIn('2022-12-21','run_task',['create_employee_payroll_report_items'])" )