[英]Python - How to run unittest without starting the application?
I have a very basic python application that I wanted to use for upskilling in unit testing. 我有一个非常基本的python应用程序,我想将其用于单元测试的升级。 The problem is when I run my test file, it starts the application. 问题是当我运行测试文件时,它将启动应用程序。 I need to shut down the application in order for the test to run and get results. 我需要关闭应用程序才能运行测试并获得结果。 I think this is something to do with the mainloop() used in the application code. 我认为这与应用程序代码中使用的mainloop()有关。 How can I just run the tests without the application starting? 在不启动应用程序的情况下如何运行测试?
Application code: 应用代码:
class User:
def __init__(self, un, pw, ut, upc):
self.username = un
self.password = pw
self.userT = ut
self.userPreferredCar = upc
self.userInfo = ""
def get_un(self):
return self.username
def get_ut(self):
return self.userT
def get_pw(self):
return self.password
def get_userPreferredCar(self):
return self.userPreferredCar
def set_userInfo(self):
self.userInfo = str("Hello " + self.username + ", you picked: " + self.userPreferredCar)
return self.userInfo
def Login():
#Create login window
login_screen = Tk()
#Create window title
login_screen.title('Login')
#Set the window size
login_screen.geometry('250x150')
#tkinter form code
login_screen.mainloop()
def signup_function():
#Get the current user
curr_user = User(username_signupE.get(), password_signupE.get(), userT.get(), carT.get())
#If the inputs are empty then throw error screen
if curr_user.get_un() == "" or curr_user.get_pw() == "" or curr_user.get_ut() == "" or curr_user.get_userPreferredCar() == "":
accCreateErr = Tk()
accCreateErr.title('Error')
accCreateErr.geometry('150x50')
errL = Label(accCreateErr, text='\nPlease fill out all details!')
errL.grid()
accCreateErr.mainloop()
else:
# Write the new user details to the user file
with open(user_file, 'w') as f:
f.write(curr_user.get_un())
f.write('\n')
f.write(curr_user.get_pw())
f.write('\n')
f.write(curr_user.get_ut())
f.write('\n')
f.write(curr_user.get_userPreferredCar())
f.close()
#Destory the signup window
signup_screen.destroy()
#Run the Login function
Login()
Test code: 测试代码:
import unittest
from main import User
from main import Car
class TestUser(unittest.TestCase):
@classmethod
def setUpClass(cls):
print('Set up User class')
@classmethod
def tearDownClass(cls):
print('Tear down User class\n')
def setUp(self):
print('Set up dummy users')
self.user_1 = User('Ryan', 'pass', 'Seller', 'Hatchback')
self.user_2 = User('Adam', 'pass', 'User', 'Van')
def tearDown(self):
print('tearDown')
def test_userInfo(self):
print('Test the user info')
self.user_1.set_userInfo()
self.user_2.set_userInfo()
self.assertEqual(self.user_1.set_userInfo(), 'Hello Ryan, you picked: Hatchback')
self.assertEqual(self.user_2.set_userInfo(), 'Hello Adam, you picked: Van')
if __name__ == '__main__':
unittest.main()
In your application code, you can check that the module is being run directly, not imported, before you start the GUI (just like you're doing in your tests): 在启动GUI之前,您可以在应用程序代码中检查模块是否在直接运行而不是导入(就像在测试中一样):
if __name__ == "__main__":
Login()
A good summary of what this code does can be found here , if you're not sure. 如果不确定,可以在这里找到有关此代码的摘要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.