I want to unittest the following function which takes a list of RunInfo objects as argument.
def get_mses(runs):
mse = []
for r in runs:
metrics = mlflow.get_run(r.run_id).data.metrics
mse.append(metrics['cv_mse'])
return mse
While the main logic is around metrics
, it does need to iterate the runs
and grab the attribute run_id
to retrieve information about each run. However I don't think I need to mock the actual RunInfo
class, but rather some fake class with an attribute run_id
. How do I mock this and create a list of objects by specifying the run_ids as simple as (1, 2, 3) using side_effects
?
I found this post, but it is not exactly what I am trying to do.
CodePudding user response:
You shouldn't have to use side_effects
for this purpose. unittest.mock
has a NonCallableMock
class that can hold arbitrary attributes.
from unittest import mock
runs = [mock.NonCallableMock(run_id=i) for i in range(1,4)]