TheGarrett
TheGarrett

Reputation: 311

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

Upvotes: 2

Views: 1823

Answers (1)

ash
ash

Reputation: 5539

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

if __name__ == "__main__":
    Login()

A good summary of what this code does can be found here, if you're not sure.

Upvotes: 3

Related Questions