Created
October 4, 2011 03:13
-
-
Save j0hn/1260828 to your computer and use it in GitHub Desktop.
GTK unittest example
This file contains hidden or 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
#!/usr/bin/env python | |
# coding: utf-8 | |
import gtk | |
class LoginWindow(gtk.Window): | |
def __init__(self): | |
gtk.Window.__init__(self) | |
self.connect("destroy", gtk.main_quit) | |
self.set_title("Login") | |
self.login_label = gtk.Label("Login please") | |
# User Input | |
self.user_input = gtk.Entry() | |
self.user_input.connect("activate", self.do_login) | |
# Password Input | |
self.pass_input = gtk.Entry() | |
self.pass_input.set_visibility(False) | |
self.pass_input.connect("activate", self.do_login) | |
self.login_hbox = gtk.HBox() | |
self.login_hbox.pack_start(self.user_input) | |
self.login_hbox.pack_start(self.pass_input) | |
self.main_vbox = gtk.VBox() | |
self.main_vbox.pack_start(self.login_label) | |
self.main_vbox.pack_start(self.login_hbox) | |
self.add(self.main_vbox) | |
self.show_all() | |
def do_login(self, *args): | |
username = self.user_input.get_text() | |
password = self.pass_input.get_text() | |
if username == "admin" and password == "admin": | |
self.login_label.set_text("Logged in") | |
self.user_input.hide() | |
self.pass_input.hide() | |
else: | |
self.login_label.set_text("Invalid username or password") | |
if __name__ == "__main__": | |
win = LoginWindow() | |
gtk.main() |
This file contains hidden or 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
#!/usr/bin/env python | |
# coding: utf-8 | |
""" | |
Tests for LoginWindow. | |
""" | |
from login import LoginWindow | |
import gtk | |
from unittest import TestCase, main | |
VALID_USER = "admin" | |
VALID_PASS = "admin" | |
def refresh_gui(): | |
while gtk.events_pending(): | |
gtk.main_iteration_do(block=False) | |
class LoginWindowTest(TestCase): | |
""" Test Cases for login.LoginWindow. """ | |
def setUp(self): | |
TestCase.setUp(self) | |
self.window = LoginWindow() | |
def test_create(self): | |
""" Creating the window should show the login form. """ | |
self.assertEqual(self.window.login_label.get_text(), "Login please") | |
self.assertEqual(self.window.user_input.get_visible(), True) | |
self.assertEqual(self.window.pass_input.get_visible(), True) | |
def test_invalid_login(self): | |
""" Can't login with an unexisting user. """ | |
self.window.user_input.set_text("user_that_does_not_exists") | |
self.window.pass_input.set_text("pass_that_does_not_exists") | |
self.window.user_input.activate() | |
refresh_gui() | |
self.assertEqual(self.window.login_label.get_text(), | |
"Invalid username or password") | |
def test_label_on_valid_login(self): | |
""" Login with a existing user. """ | |
self.window.user_input.set_text(VALID_USER) | |
self.window.pass_input.set_text(VALID_PASS) | |
self.window.user_input.activate() | |
refresh_gui() | |
self.assertEqual(self.window.login_label.get_text(), "Logged in") | |
def test_inputs_on_valid_login(self): | |
""" Login with a existing user. """ | |
self.window.user_input.set_text(VALID_USER) | |
self.window.pass_input.set_text(VALID_PASS) | |
self.window.user_input.activate() | |
refresh_gui() | |
self.assertEqual(self.window.user_input.get_visible(), False, | |
"Username input is visible") | |
self.assertEqual(self.window.pass_input.get_visible(), False, | |
"Password input is visible") | |
def test_password_visibility(self): | |
""" Password text should not be visible for reading. """ | |
self.assertEqual(self.window.pass_input.get_visibility(), False, | |
"Password text is visible") | |
self.window.pass_input.set_text("just some random text") | |
self.assertEqual(self.window.pass_input.get_visibility(), False, | |
"Password text is visible") | |
def tearDown(self): | |
TestCase.tearDown(self) | |
# TODO: fix RuntimeError: called outside of a mainloop | |
self.window.destroy() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix for tearDown: