Testing our model

Our CSVModel code is fairly self-contained apart from its need to read and write files. Since file operations are one of the more common things that need to be mocked out in a test, the mock module provides mock_open, a Mock subclass ready-made to replace Python's open method. When called, a mock_open object returns a mock file handle object, complete with support for the read(), write(), and readlines() methods.

Let's begin creating our test case class in test/test_models.py as follows:

from .. import modelsfrom unittest import TestCasefrom unittest import mockclass TestCSVModel(TestCase):    def setUp(self):        self.file1_open = mock.mock_open(            read_data=(                "Date,Time,Technician,Lab,Plot,Seed sample,Humidity,Light," "Temperature,Equipment ...

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.