Testing file reading in get_all_records()

To see how we use these, let's start a test for the get_all_records() method as follows:

    @mock.patch('abq_data_entry.models.os.path.exists')
    def test_get_all_records(self, mock_exists):
        mock_exists.return_value = True

Since our filenames don't actually exist, we're using the decorator version of patch to patch os.path.exists with a mock function that always returns True. We can later change the return_value value if we want to test a scenario where the file doesn't exist.

To run the get_all_records() method, we'll use the context manager form of patch() as follows:

        with mock.patch('abq_data_entry.models.open', self.file1_open):
            records = self.model1.get_all_records()

Any call to open() inside the ...

Get Python GUI Programming with Tkinter now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.