0

Below is the how the program is set up. I have a UI that is (naturally) waiting for user input.

root = Tk()
root.title("This space intentionally left blank")
mainFrame = Frame(root)
mainFrame.grid(column=1, row=2)
sideFrame=Frame(root)
sideFrame.grid(column=2, row=2)
topLabelFrame=Frame(root)
topLabelFrame.grid(column=1, row=1, columnspan=99)
main()
root.mainloop()

This is my unit test:

from unittest import TestCase
from AL2.AutoLinker2_0 import InputProcessor


    class TestInputProcessor(TestCase):

        def test_tokenize(self):
            IP = InputProcessor("")
            self.assertEqual(IP.tokenize("elbow mac"), ["elbow", "mac"])

When I run, my UI pops up and hangs up the unit tests until I close the UI. I'm new to unit testing, but my understanding was that a unit test should only test the class and shouldn't need to run the entire program. Is this me not understanding, or is this maybe an issue with the IDE (pycharm), or is my program set up wrong? Thanks!

2
  • 2
    If you do not have your code in module format and you are importing it into your unit test, it would run the GUI code. Make sure you use an if name == "main" conditional Commented Mar 9, 2016 at 0:53
  • Tanner, I wish you would post this as an answer so I could accept it. Commented Mar 9, 2016 at 18:43

1 Answer 1

2

When importing a Python file as a module any code in the global scope will be executed upon parsing the file. This also applies to any code inside a class.

To allow your file to be both imported and callable, you need to use:

#!/usr/bin/python3
x = 1
y = 3

if "__name__" == "__main__":
    print(x+y)

__name__ always contains the name of the current module, except when a module is being executed, in this case, it will have the name "__main__"

For more info see: http://effbot.org/pyfaq/tutor-what-is-if-name-main-for.htm or https://www.ibiblio.org/swaroopch/byteofpython/read/module-name.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.