Why not use the side_effect parameter in your mock.patch calls below? That way you don't have to do this, and you also don't have to set the side_effect inside the test body.