Simulating the passing of time in unittesting

后端 未结 3 1978
醉话见心
醉话见心 2021-02-04 03:25

I\'ve built a paywalled CMS + invoicing system for a client and I need to get more stringent with my testing.

I keep all my data in a Django ORM and have a bunch of Cele

3条回答
  •  太阳男子
    2021-02-04 04:00

    You can use mock to change the return value of the function you use to get the time (datetime.datetime.now for example).

    There are various ways to do so (see the mock documentation), but here is one :

    import unittest
    import datetime
    from mock import patch
    
    class SomeTestCase(unittest.TestCase):
        def setUp(self):
            self.time = datetime.datetime(2012, 5, 18)
            class fakedatetime(datetime.datetime):
                @classmethod
                def now(cls):
                    return self.time
            patcher = patch('datetime.datetime', fakedatetime)
            self.addCleanup(patcher.stop)
            patcher.start()
    
        def test_something(self):
            self.assertEqual(datetime.datetime.now(), datetime.datetime(2012, 5, 18))
            self.time = datetime.datetime(2012, 5, 20)
            self.assertEqual(datetime.datetime.now(), datetime.datetime(2012, 5, 20))
    

    Because we can't replace directly datetime.datetime.now, we create a fake datetime class that does everything the same way, except returning a constant value when now is called.

提交回复
热议问题