How to mock python's datetime.now() in a class method for unit testing?

前端 未结 7 1230
南旧
南旧 2020-12-14 05:38

I\'m trying to write tests for a class that has methods like:

import datetime
import pytz

class MyClass:
    def get_now(self, timezone):
        return dat         


        
相关标签:
7条回答
  • 2020-12-14 06:33

    I would use the helpers from the 'testfixtures' package to mock out the datetime class you're calling now() on:

    http://packages.python.org/testfixtures/datetime.html#datetimes

    That way, you can test all the cases you have, all the time.

    0 讨论(0)
提交回复
热议问题