I have a simple file program (removed code from functions since its irrelevant for the problem):
import random
import sys
def poziom(level):
*does something and returns something*
def gra(chance):
*does something and returns something*
def save(name):
*does something and returns something*
while True:
*does something in loop, executing other functions above*
I'm trying to run a simple unit test against one of its functions:
import unittest
from my_game import save
from my_game import gra
from my_game import poziom
class TestSum(unittest.TestCase):
def test_list_int(self):
"""
Test that it can sum a list of integers
"""
check = "Test string"
result = poziom(check)
self.assertEqual(result, 0)
if __name__ == '__main__':
unittest.main()
However, this test instead of running a poziom() function, runs through entire file, also executing the code behind the While True statement. Do I do something wrong? Is it possible in such case to write a test that checks ONLY one function and value returned by it?
while True:in anif __name__ == "__main__":statement, does that fix it? Otherwise, just importing that file will execute the contents of that loop.