Home > Blockchain >  Unittest hangs when testing input, but same input runs perfectly fine on main program
Unittest hangs when testing input, but same input runs perfectly fine on main program

Time:11-09

Whenever I run the following unittest, it just hangs and nothing happens. The program itself returns an output withing 1 sec, but I've ran this for 5 mins without anything happening.

import unittest

from syntax import *


class SyntaxTest(unittest.TestCase):
    def testCase(self):
        self.assertEqual(CheckSyntax('C'), 'Formeln är syntaktiskt korrekt')

if __name__ == '__main__':
    unittest.main() 

The code itself is a simple syntax checker for molecules. It's not completely finished yet, but runs fine for the specified input above ('C'). I've uploaded it here: https://pastebin.com/rD3f6PWL

CodePudding user response:

You are importing your syntax module, that means it will run all the function and class definitions (not the code inside them) and any other code in the main body. the last line of the syntax module is

print(CheckSyntax(input()))

So when you import the syntax module this line will be executed and will be waiting for input. If you only mean this line to be executed when you run syntax as a script then you would be better to wrap it in an if main block like

if __name__ == "__main__":
    print(CheckSyntax(input()))

this way the last line will only be run if the script is being run directly and not if its being imported to be used somewhere else.

  • Related