Mocking date and time in LiveServerTestCase?

56 views
Skip to first unread message

Carsten Fuchs

unread,
Jan 25, 2017, 2:47:02 PM1/25/17
to django...@googlegroups.com
Dear Django fellows,

I'm using LiveServerTestCase with Selenium. The application code run by
LiveServerTestCase's live server works well and a view's call to date.today()
returns the current date.

For the tests, however, it would be helpful if we could fix the time and date at
which the test is supposedly run, making date.today() and similar functions
return a predetermined value.

Can this be achieved?

Best regards,
Carsten

Adam Stein

unread,
Jan 25, 2017, 3:01:59 PM1/25/17
to django...@googlegroups.com
When I need to set the date to a known day for tests, I use forbiddenfruit (https://github.com/clarete/forbiddenfruit).

Carsten Fuchs

unread,
Jan 30, 2017, 12:11:01 PM1/30/17
to django...@googlegroups.com
Hi Adam,

Am 25.01.2017 um 21:01 schrieb Adam Stein:
> When I need to set the date to a known day for tests, I use forbiddenfruit
> (https://github.com/clarete/forbiddenfruit).

Thanks for mentioning this, I didn't know it before!

However, I have not yet tried it, because I had hoped that there was a less
intrusive method to achieve similar results and mostly because I still find it
difficult to understand the ramifications of this approach with functional
tests, that is, when LiveServerTestCase takes the server part (to be affected by
forbiddenfruit) while the test case itself uses Selenium (and should, at least
in theory, not be affected, e.g. for taking time measurements or log time stamps).

Alas, I guess that I'll just give up and write my tests in a work-around manner
that circumvents details that depend on server time…

Best regards,
Carsten
Reply all
Reply to author
Forward
0 new messages