【问题标题】:Python - How to run unittest without starting the application?Python - 如何在不启动应用程序的情况下运行单元测试?
【发布时间】:2018-01-31 00:04:51
【问题描述】:

我有一个非常基本的 python 应用程序,我想用它来提高单元测试的技能。问题是当我运行我的测试文件时,它会启动应用程序。我需要关闭应用程序才能运行测试并获得结果。我认为这与应用程序代码中使用的 mainloop() 有关。如何在不启动应用程序的情况下只运行测试?

应用代码:

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()

测试代码:

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()

【问题讨论】:

  • 请发布MCVE,显示您的应用程序在您运行测试时启动。特别是,我们需要从main.py 中查看更多信息,或者您是否有任何代码在__init__.py 文件中运行。

标签: python unit-testing


【解决方案1】:

在您的应用程序代码中,您可以在启动 GUI 之前检查模块是否正在直接运行,而不是导入(就像您在测试中所做的那样):

if __name__ == "__main__":
    Login()

如果您不确定,可以在 here 找到此代码功能的一个很好的总结。

【讨论】:

  • 谢谢伙计,这成功了!是的,在我的应用程序代码中我没有,我只是在使用: if os.path.isfile(user_file): Login() else: #Else run the signup window Signup()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-23
  • 1970-01-01
  • 2015-05-31
  • 2023-01-09
  • 1970-01-01
  • 2020-04-15
相关资源
最近更新 更多