Skip to content Skip to sidebar Skip to footer

Stop Unittest From Running Code From Import Module

Got 2 files : new_project ├── Main.py └── testing.py I run: $ cd new_project $ python -m unittest testing.py My whole testing file is : import unittest from Main im

Solution 1:

To avoid running the loop, when the file is imported as a module, write it this way:

if __name__ == "__main__":
    while True:
        # do stuff every turn
        x, y, value = [int(j) for j in input().split()]

The answer is simple, when you import the module the code on that file will be executed, which will cause the loop to run and the execution to be blocked on the input(). For more details about how it works please read this answer with a detailed explanation


Post a Comment for "Stop Unittest From Running Code From Import Module"