views:

28

answers:

2

Hey, I am helping to set up a regression testing suite for our python web application. Many of our tests are scheduling style tests where the current date is important. For example: create a recurring event that runs every week for a month starting on Feb 1.

In order to test this, what I really want to do is override the current date so I can move back and forward in time to check the state of the app. For example, I may add an test-only page that lets me set the 'current' date which gets passed to the python back end and is used for date calculations.

In the past when I have done this, I engineered it into the application architecture from day 1. Unfortunately, I am coming in late to this project and there is no application support for this.

So here's my question, is there any way I can override the current date on a web service call? For example, can I intercept calls for the current date (monkey patching possibly?). I would rather not have to do a whole IOC thing as it would mean changing hundreds of methods. - dave

+1  A: 

You could do something like this:

from datetime import datetime
orig_datetime_now = datetime.now

datetime.now = lambda: datetime(2010, 2, 1)

# tests here

datetime.now = orig_datetime_now

Though, it's probably a better idea (as not to screw with third party libraries, etc.) to put a helper method on your class to get the current date, and do the same monkey patching as show above.

sdolan