Python: How do I mock datetime.utcnow()?

后端 未结 5 1025
梦如初夏
梦如初夏 2020-12-30 02:24

I have the below:

from datetime import datetime

def get_report_month_key():
    month_for_report = datetime.utcnow()
    return month_for_report.strftime(\"         


        
5条回答
  •  伪装坚强ぢ
    2020-12-30 02:46

    The accepted answer by dasjotre works if you don't create any datetime instances in the module you are testing. If you try to create a datetime it will create a Mock object instead of one with the expected methods on a standard datetime object. This is because it replaces the whole class definition with a mock. Instead of doing this, you can use a similar approach to create the mocked definition by using datetime as the base.

    mymodule.py

    from datetime import datetime
    
    def after_y2k():
        y2k = datetime(2000, 1, 1)
        return y2k < datetime.utcnow()
    

    test_mymodule.py

    import unittest
    import datetime
    from mock import patch, Mock
    import mymodule
    from mymodule import after_y2k
    
    
    class ModuleTests(unittest.TestCase):
        @patch.object(mymodule, 'datetime', Mock(wraps=datetime.datetime))
        def test_after_y2k_passes(self):
            # Mock the return and run your test (Note you are doing it on your module)
            mymodule.datetime.utcnow.return_value = datetime.datetime(2002, 01, 01)
            self.assertEqual(True, after_y2k())
    
            mymodule.datetime.utcnow.return_value = datetime.datetime(1999, 01, 01)
            self.assertEqual(False, after_y2k())
    
        @patch('mymodule.datetime')
        def test_after_y2k_fails(self, mock_dt):
            # Run your tests
            mock_dt.utcnow = Mock(return_value=datetime.datetime(2002, 01, 01))
            self.assertEqual(True, after_y2k())
    
            # FAILS!!! because the object returned by utcnow is a MagicMock w/o 
            # datetime methods like "__lt__"
            mock_dt.utcnow = Mock(return_value=datetime.datetime(1999, 01, 01))
            self.assertEqual(False, after_y2k())
    

提交回复
热议问题