mercredi 13 juin 2018

How to test a function that is using user-interface

I am trying to test a function that needs an interaction of the user. The question is how can I do it programmatically ?

Here is an example where we ask for the user to select an item in a list (main.py) :

import tkinter as tk
from tkinter import Button, OptionMenu, StringVar


def ask_for_item_in_list(lst, title, default_index=0):

    root, item = tk.Tk(), None
    WIDTH, HEIGHT = 300, 120

    root.title(title)

    root.maxsize(width=WIDTH, height=HEIGHT)
    root.minsize(width=WIDTH, height=HEIGHT)
    root.resizable(0, 0)

    variable = StringVar(root)
    variable.set(lst[default_index])

    option_menu = OptionMenu(root, variable, *lst)
    option_menu.pack(fill="none", expand=True)

    def on_close():
        # The window has been closed by the user
        variable.set(None)
        close()

    def close():
        # It quits mainloop()
        root.quit()
        # It closes the window
        root.destroy()

    button_ok = Button(root, text='OK', command=close)
    button_ok.pack(fill='none', expand=True)

    root.protocol('WM_DELETE_WINDOW', on_close)

    # Execution stops here as long as the user has not closed the window or
    # pressed ok
    root.mainloop()

    # We retrieve the selected item
    item = variable.get()
    if item == 'None':
        item = None

    return item

if __name__ == '__main__':
        lst = ['Item 1', 'Item 2', 'Item 3']
        title = 'Select an item'
        default_selected_idx = lst.index('Item 2')

        selected_item = ask_for_item_in_list(lst, title, default_selected_idx)

        print(selected_item)

I used pytest to write all my tests since I can't use object oriented programming. Actually, the code must be maintainable by people who are not professional developpers.

As you can see, I can't test this function this way, since it will wait for the user input (test_main.py):

from main import ask_for_item_in_list


def test_ask_for_item_in_list():
    lst = ['Item 1', 'Item 2', 'Item 3']
    title = 'Select an item'

    # Here TRY to test if changing the default selected index works
    default_selected_idx = lst.index('Item 2')

    # Code to simualte that the user as clicked on OK ?
    # user.click_button('OK') ?
    selected_item = ask_for_item_in_list(lst, title, default_selected_idx)

    assert selected_item == 'Item 2'

  • Do I need to change the way I have coded this ?
  • Is it relevant to test this kind of function ?

I have faced this problem many times (whatever is the language used), I would like to know how this is supposed to be done in a clean way.

Thanks for reading ! :)

Aucun commentaire:

Enregistrer un commentaire