Testing our mixin class

One additional challenge we haven't approached yet is testing our mixin class. Unlike our other widget classes, our mixin cannot really exist on its own: it depends on methods and properties found in the ttk widget which it's combined with.

One approach to testing this class would be to mix it with a Mock object which mocks out any inherited methods. This approach has merit, but a simpler (if less ideal) approach is to subclass it with the simplest possible ttk widget and test the resulting child class.

That approach looks like this:

class TestValidatedMixin(TkTestCase):

    def setUp(self):
        class TestClass(widgets.ValidatedMixin, ttk.Entry):
            pass
        self.vw1 = TestClass(self.root)

Here, we've created just a basic child class ...

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.