I’m looking into how I can test a flow with mocked...
# ask-metaflow
s
I’m looking into how I can test a flow with mocked functions. This is because some of the functions will have API calls that I want to control the responses to for testing. I believe the recommended approach is to use subprocess but that doesn’t seem to be able to use any of the mocked values I establish before I call it. Any recommendations? Thank you!
1
v
take a look at this article that highlights a few patterns do you want to mock artifact values, parameters, or something else?
s
I want to mock a call that is made to an API in a step. for example:
Copy code
@step
def get_data_from_api(self):
  # this API call I'd like to be mocked so I can control the response
  results = make_api_call()
  self.next(self.end)
👀 1
And thank you so much @straight-shampoo-11124 for all your help!
👍 1
v
are you using a library/framework for mocking or doing it manually?
s
I’m currently just testing and using
unittest.mock.patch
to explore using Metaflow. The library I use in our code is a combonation of
VCR
and
unittest.mock.patch
I built some pretty hacky patching code for
metaflow.runtime.NativeRuntime._launch_worker
and
metaflow.runtime.NativeRuntime._poll_workers
which let me bypass calling subprocess and instead call back into
cli.main
. Would you be interested in taking a look?
v
sure!
s
@straight-shampoo-11124 not to pester, just wanted to bump this in case it’s at all helpful ☝️ — I’m going to be using this to test my flows as I think I’ve sold folks enough at my company to take Metaflow for a spin 😄
v
sorry for the slow response here! this makes sense 🙂 Are you ok going with this approach for mocking for now?
s
yup 😄 — happy to use some custom code for now
👍 1
@straight-shampoo-11124 if I want to help drive forward the ability to have a local runner (obviously with different code that doesn’t patch 🤦 ) any recommendations on how I should go about that? I’m unsure my time availability, but would love to hear any recommendations you have, in case time is available
s
you mean a programmatic API for running Metaflow commands? It's definitely something we want to do. Have you seen this memo on the topic?
s
I’ve seen it before, but looking through it again now with current context 😄