Created
December 17, 2018 20:31
-
-
Save initialed85/d6e8c9fa83c0889acd2b605c1b59ef84 to your computer and use it in GitHub Desktop.
Tkinter mock UI test example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import traceback | |
from PyQt5.QtGui import QFontDatabase | |
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QVBoxLayout, QLabel, QTextEdit | |
_STATE_LABEL_PREFIX = 'State: ' | |
class WoWHMI(object): | |
def __init__(self, command, command_callback=None): | |
self.command = command.strip() | |
self.command_callback = command_callback | |
self.app = QApplication([]) | |
try: | |
self.app.setStyle('Macintosh') | |
except Exception: | |
self.app.setStyle('Fusion') | |
self.window = QWidget() | |
self.window.setWindowTitle( | |
'{} - {}'.format(self.__class__.__name__, self.command) | |
) | |
self.window.resize(640, 480) | |
self.text_log = QTextEdit() | |
self.text_log.setFont(QFontDatabase.systemFont(QFontDatabase.FixedFont)) | |
self.label_state = QLabel('{}{}'.format(_STATE_LABEL_PREFIX, 'unknown')) | |
self.button = QPushButton(self.command) | |
self.button.clicked.connect(self.on_click_button) | |
self.layout = QVBoxLayout() | |
self.layout.addWidget(self.text_log) | |
self.layout.addWidget(self.label_state) | |
self.layout.addWidget(self.button) | |
self.window.setLayout(self.layout) | |
self.window.show() | |
def on_click_button(self): | |
message = self.command.strip().lower() | |
log_line = '{} - {} - {}'.format( | |
datetime.datetime.now(), | |
'sending command', | |
repr(message) | |
) | |
if self.command_callback is not None: | |
try: | |
log_line += ' - {}'.format(repr(self.command_callback)) | |
self.command_callback(message) | |
except Exception: | |
log_line += '\n{}'.format(traceback.format_exc()) | |
else: | |
log_line += ' - callback not given!' | |
self.text_log.append(log_line) | |
def set_state(self, state): | |
self.text_log.append('{} - {} - {}'.format( | |
datetime.datetime.now(), | |
'received state', | |
repr(state) | |
)) | |
self.label_state.setText('{}{}'.format(_STATE_LABEL_PREFIX, state)) | |
def run(self): | |
self.app.exec_() | |
if __name__ == '__main__': | |
import sys | |
command = None | |
try: | |
command = sys.argv[1] | |
except Exception: | |
print('error: need to pass in command string as first argument') | |
exit(1) | |
def command_callback(command): | |
print(repr(command)) | |
app = WoWHMI( | |
command, | |
command_callback, | |
) | |
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from PyQt5.QtWidgets import QTextEdit | |
from hamcrest import assert_that, equal_to | |
from mock import Mock, call | |
from hmi.hmi.app import WoWHMI | |
def command_callback(command): | |
pass | |
class WoWHMITest(unittest.TestCase): | |
def setUp(self): | |
self.command_callback = Mock(spec=command_callback) | |
self.subject = WoWHMI( | |
command='some_command', | |
command_callback=self.command_callback | |
) | |
def test_on_click_button(self): | |
self.subject.text_log = Mock(spec=QTextEdit) | |
self.subject.on_click_button() | |
assert_that( | |
self.command_callback.mock_calls, | |
equal_to([ | |
call('some_command') | |
]) | |
) | |
assert_that( | |
len(self.subject.text_log.append.mock_calls), | |
equal_to(1), | |
) | |
def test_on_click_button_no_command_callback(self): | |
self.subject.text_log = Mock(spec=QTextEdit) | |
self.subject.command_callback = None | |
self.subject.on_click_button() | |
assert_that( | |
self.command_callback.mock_calls, | |
equal_to([]) | |
) | |
assert_that( | |
len(self.subject.text_log.append.mock_calls), | |
equal_to(1), | |
) | |
def test_set_state(self): | |
self.subject.text_log = Mock(spec=QTextEdit) | |
self.subject.label_state = Mock(spec=QTextEdit) | |
self.subject.set_state('some state') | |
assert_that( | |
len(self.subject.text_log.append.mock_calls), | |
equal_to(1) | |
) | |
assert_that( | |
self.subject.label_state.setText.mock_calls, | |
equal_to([call('State: some state')]) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment