0

I have a class named Tower:

class Tower:
    def __init__(self, # params..):
        # some settings of variables 
        add_game_turn_event(self._update)
    def turn_result(self):
        # some action

as you can see, turn_result is subscribed to a collection of methods which invokes every x seconds:

game_turn_events = []

def add_game_turn_event(function):
    game_turn_events.append(function)

async def _game_turn():
    while True:
        threading.Timer(sec_per_turn, _game_turn).start()
        for function in game_turn_events:
            function()

now I want to write a unit test (using the unittest module) that creates a tower, and then checks how the tower has changed after turn_result is invoked once. I don't really know how to do it. I though about something like:

def test_tower_updating(self):
    tower = Tower( # params..)
    add_game_turn_event(lambda: self._test_tower_updating(tower, 10 + levels[2].yield_per_turn))

def _test_tower_updating(self, tower, expected):
    self.assertTrue(tower.warriors_group.soldiers_amount == expected)

but this does not wait for the function to be invoked and gives false positive anyways. the problem is, that _game_turn() is async, so it dosen't wait for it to finish. await won't work here, because I dont call _game_turn() directly, rather I update a global variable it uses.

How can I test such thing?

avivgood2
  • 227
  • 3
  • 19
  • Have a look at [how to test python 3.4 asyncio code](https://stackoverflow.com/questions/23033939/how-to-test-python-3-4-asyncio-code) - there are some answers that may help you. – MrBean Bremen Sep 14 '20 at 19:21
  • @MrBeanBremen as I said, asinco is called indirectly so it won't work – avivgood2 Sep 14 '20 at 19:28

0 Answers0