Open laserprec opened 2 years ago
Complex objects that don't have a serialization, like dataframes and mocks, need to be explicitly injected. We need to brainstorm some easier ways to represent complex object mock pass down, perhaps testing and enabling mock object side-effects.
But ultimately to make this work you'll need to tb.inject
the code that's needed to do the mocking:
tb.inject('''
mock_df = pd.DataFrame(np.random.randint(0,100,size=(100, 4)))
unittest.mock.patch('__main__.load_csv_from_internet', return_value=mock_df)
''')
See https://github.com/nteract/testbook/issues/72 for some further notes on the topic.
Is it possible to patch an object and set its return_value to another object?
For example let's say a Jupyter Notebook defines a method that downloads some CSV from the internet and load it in as a
pandas.DataFrame
.When writing a unit test for such method, I would like to avoid the making the external call and probably patch the
return_value
ofload_csv_from_internet()
to some fakepandas.DataFrame
I pre-computed. For example:Unfortunately, at the moment when assigning the
return_value
to an object, the object is casted to string type leading to the following error:Is there plan to support returning objects as it is?