Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Pytest: Mocking slow external API call

import externalapi

def compute(x, y):
    xx = externalapi.remote_compute(x)
    yy = externalapi.remote_compute(y)
    result = (xx+yy) ** 0.5
    return result
import time

def remote_compute(x):
    time.sleep(5) # to imitate long running process
    return x*x

import mymath

print(mymath.compute(3, 4))