Mocking open(file_name) in unit tests

前端 未结 8 1813
慢半拍i
慢半拍i 2020-12-05 00:37

I have a source code that opens a csv file and sets up a header to value association. The source code is given below:

def ParseCsvFile(source): 
  \"\"\"Pa         


        
8条回答
  •  自闭症患者
    2020-12-05 01:01

    @mock.patch decorator (2.7 example)

    This is now much easier:

    import your_script.py
    import __builtin__
    import mock
    
    
    @mock.patch("__builtin__.open")
    def test_example(self, mock_open):
        your_script.your_method()
        self.assertEqual(mock_open.call_count, 1)
    

提交回复
热议问题