2

Is it possible to make function with the name return that will simulate normal python return.

Why: So that in unittesting we can break from the method that have infinity loop:

while True:
    ...
    time.sleep(x)

I want to do something like this...

def return(y):
    ...

self.mock_module['time'].sleep.side_effect = [return(z)]

2 Answers 2

2

No. That is not possible. However, you can do something like this:

condition = []
while not condition:
    ...

self.mock_module['time'].sleep.side_effect = [lambda:condition.append(1)]
Sign up to request clarification or add additional context in comments.

Comments

0

You can't have return as a function – because that function would simply return from itself. I think the solution you're looking for is an exception:

class StopTest(Exception):
    pass

def stopTest():
    raise StopTest()

...

    self.mock_module['time'].sleep.side_effect = [stopTest]

and then use assertRaises to expect the StopTest exception in your unit test.

2 Comments

Well, there's no accounting for taste. ;-)
Well nothing personal but but exception calling in side_effect is kinda dirty. :P. But yea you are right :D.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.