How to test time-related functions in Django?
I am writing an app that records time of events. For unit testing I would usually use a monkey-patch to replace datetime.time
with a fake so I can test it properly. I am trying to do end-to-end tests with Selenium, with the test cases in a separate program, not using python manage.py t开发者_开发知识库est
. Therefore I can't do a patch. I did try using manage.py but it did not seem to help.
I'm sure this is a solved problem. How should I be doing it? Is Selenium just not the right tool for this sort of testing? Am I missing how to get the test case to talk to the application?
Selenium talks to a full webserver and has no access to the python interpreter running inside that webserver. Even if you are scripting SeleniumRC with python, the script instance of the interpreter is separate from the webserver instance.
If you are running the test webserver via manage.py runserver, you could write your own management command to replace 'runserver' with a version that patches datetime.time. This won't be easy, so you may consider either revising your Selenium-driven tests to cope with events happening in realtime, or convert you time-sensitive tests to django client tests so you can use the mock library.
精彩评论