Skip to content

Commit

Permalink
Amend Al's tests so that they run under Travis CI
Browse files Browse the repository at this point in the history
We actively advertise that easygui has few dependencies.
* switch pytest -> unittest in order to reduce dependences
* refactor to work with unittest (no parametrize, must derive from unittest.TestCase class)

And also:
* add init file so that the tests get discovered by unittest
* add pytest to the requirements.txt
* and use a sufficiently high version to avoid moses-palmer/pynput#173

I would LOVE to remove the pynput dependency too, but that library is just too darn beautiful.
It does exactly what we want (kudos: Al for using it here) and is totally cross platform. After a few hours looking, I can't
find a way to integration this nicely without massive changes.

Top alternatives:
* using mocking and testing through the box-object instantiation rather than the get-and-block-and-return call
* refactor tests to do something like this: https://stackoverflow.com/questions/4083796/how-do-i-run-unittest-on-a-tkinter-app
* re-implement the necessary keyboard functionality from pynput to do nice cross platform testing without the import
  • Loading branch information
zadacka committed May 3, 2021
1 parent c462d43 commit 4de6c71
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 57 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pynput==1.7.3 # required for keypress simulation for Al's integration tests
Empty file added test_cases/__init__.py
Empty file.
116 changes: 59 additions & 57 deletions test_cases/test_easygui.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

sys.path.append('..')
import easygui

Expand All @@ -7,12 +8,13 @@
import time
import threading

import pytest
import unittest

from pynput.keyboard import Key, Controller

KEYBOARD = Controller()
FOLDER_OF_THIS_FILE = os.path.dirname(os.path.abspath(__file__))
GUI_WAIT = 0.6 # if tests start failing, maybe try bumping this up a bit (though that'll slow the tests down)
GUI_WAIT = 0.6 # if tests start failing, maybe try bumping this up a bit (though that'll slow the tests down)


"""
Expand All @@ -39,58 +41,58 @@ def run(self):
KEYBOARD.type(self.keyPresses)


def test_test_images_exist():
assert os.path.exists(os.path.join(FOLDER_OF_THIS_FILE, 'pi.jpg'))
assert os.path.exists(os.path.join(FOLDER_OF_THIS_FILE, 'result.png'))

@pytest.mark.parametrize(
'args,kwargs,expected',
(
((), {}, 'OK'), # msgbox with no arguments
(('Message',), {}, 'OK'), # with custom message
(('Message', 'Title'), {}, 'OK'), # custom message and title
((), dict(ok_button='Button'), 'Button'), # custom button text
(('Message', 'Title'), dict(ok_button='Button'), 'Button'), # combo of all three
((), dict(image=os.path.join(FOLDER_OF_THIS_FILE, 'pi.jpg')), 'OK'), # test jpg
((), dict(image=os.path.join(FOLDER_OF_THIS_FILE, 'result.png')), 'OK'), # test png
),
)
def test_spacebar_clicks_choice(args, kwargs, expected):
"""
Test that the spacebar selects a choice.
Parameterized across several cases, customizing msg, title, etc.
"""

t = KeyPresses(' ')
t.start()
assert easygui.msgbox(*args, **kwargs) == expected

def test_buttonbox():
# Test hitting space to click OK with different default buttons:
t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[1]') == 'Button[1]'

t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[2]') == 'Button[2]'

t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[3]') == 'Button[3]'

# Test hitting Esc to close.
# TODO: If button boxes aren't given a default choice, then their window won't be in focus and this test hangs.
#t = KeyPresses([Key.esc])
#t.start()
#print('Line', inspect.currentframe().f_lineno)
#assert easygui.buttonbox() is None

# Test hitting Esc to close.
t = KeyPresses([Key.esc])
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox(default_choice='Button[1]') is None
class IntegrationTests(unittest.TestCase):

def test_test_images_exist(self):
assert os.path.exists(os.path.join(FOLDER_OF_THIS_FILE, 'pi.jpg'))
assert os.path.exists(os.path.join(FOLDER_OF_THIS_FILE, 'result.png'))

def test_spacebar_clicks_choice(self):
"""
Test that the spacebar selects a choice.
Parameterized across several cases, customizing msg, title, etc.
"""
parameters = (
((), {}, 'OK'), # msgbox with no arguments
(('Message',), {}, 'OK'), # with custom message
(('Message', 'Title'), {}, 'OK'), # custom message and title
((), dict(ok_button='Button'), 'Button'), # custom button text
(('Message', 'Title'), dict(ok_button='Button'), 'Button'), # combo of all three
((), dict(image=os.path.join(FOLDER_OF_THIS_FILE, 'pi.jpg')), 'OK'), # test jpg
((), dict(image=os.path.join(FOLDER_OF_THIS_FILE, 'result.png')), 'OK'), # test png
)

for args, kwargs, expected in parameters:
k = KeyPresses(' ')
k.start()
assert easygui.msgbox(*args, **kwargs) == expected

def test_buttonbox(self):
# Test hitting space to click OK with different default buttons:
t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[1]') == 'Button[1]'

t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[2]') == 'Button[2]'

t = KeyPresses(' ')
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox('Message', 'Title', choices=('Button[1]', 'Button[2]', 'Button[3]'), default_choice='Button[3]') == 'Button[3]'

# Test hitting Esc to close.
# TODO: If button boxes aren't given a default choice, then their window won't be in focus and this test hangs.
#t = KeyPresses([Key.esc])
#t.start()
#print('Line', inspect.currentframe().f_lineno)
#assert easygui.buttonbox() is None

# Test hitting Esc to close.
t = KeyPresses([Key.esc])
t.start()
print('Line', inspect.currentframe().f_lineno)
assert easygui.buttonbox(default_choice='Button[1]') is None

0 comments on commit 4de6c71

Please sign in to comment.