mercredi 17 février 2021

Test Python Tkinter GUI application without displaying elements

How to test tkinter gui without displaying elements? This should make a test quicker. Code below works, but one has to close tkinter windows after runing test to make them ends, is there a possibility to test Next class without running tkinter app or just do not display it? I heard about unittest.mock, but when i try to mock tk.Tk() it crashes. Related: pygtk test, unittest gtk, test tkinter - old explantation

 import tkinter as tk


class Base(tk.Toplevel):
    def __init__(self, parent, title):
        tk.Toplevel.__init__(self.parent)
        self.transient(parent)
        self.overrideredirect(1)
        self.parent = parent
        self.title = title
        color = '#%02x%02x%02x' % (122, 17, 117)
        self.canvas = tk.Canvas(self, bg=color, relief='raised')
        # etc.


class Next(Base, object):
    def __init(self, parent, title, color='white', **kwargs):
        Base.__init__(parent, title)
        kwargs['cancel'] = self.cancel
        kwargs['tk_entry_color'] = color
        self.pg = KeyPage(self.canvas, title, **kwargs)

    def cancel(self):
        self.pg.clean()

And test class:

class TestNext(unittest.TestCase):

@classmethod
def setUpClass(cls):
    pass

@classmethod
def tearDownClass(cls):
    pass

def setUp(self):
    self.root = tk.Toplevel()
    self.title = 'test_next'
    self.kwargs = {'cancel': None}


def tearDown(self):
    self.next = None
    if self.root:
        self.root.destroy()

def test_init(self):
    page = Next(self.root, self.title, **self.kwargs)

Aucun commentaire:

Enregistrer un commentaire