override python function-local variable in unittest

前端 未结 2 606
太阳男子
太阳男子 2021-02-05 03:59

I have a method in python (2.7) that does foo, and gives up after 5 minutes if foo didn\'t work.

def keep_trying(self):
    timeout = 300  #empirically derived,          


        
2条回答
  •  猫巷女王i
    2021-02-05 04:51

    You can't mock a function's local variable. To make your code easier to test, change it to, e.g:

    def keep_trying(self, timeout=300):
        end_time = time.time() + timeout
        # etc, as above
    

    so it becomes trivial for tests to run it with a shorter timeout!

提交回复
热议问题