You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
importpandasaspdimportrequestsdefload_csv_from_internet():
csv_file=requests.get(....) # fetch some CSV from the internet
...
returnpd.read_csv(csv_file) # read it and return as 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 of load_csv_from_internet() to some fake pandas.DataFrame I pre-computed. For example:
#setup a fake pandas.DataFramemock_df=pd.DataFrame(np.random.randint(0,100,size=(100, 4)))
# patching the `load_csv_from_internet` method from the Notebookwithtb.patch('__main__.load_csv_from_internet', return_value=mock_df) asmock_data:
.... # some assertion check
Unfortunately, at the moment when assigning the return_value to an object, the object is casted to string type leading to the following error:
from unittest.mock import patch
E _patcher_yrvzguhxnm = patch(
E "__main__.load_csv_from_internet",
E **{"return_value": " 0 1 2 3E 0 2 66 60 22E 1 87 1 65 54E 2 95 99 7 20E 3 45 84 48 66E 4 85 96 85 23E .. .. .. .. ..E 95 79 66 35 94E 96 91 50 43 87E 97 5 84 14 18E 98 13 30 23 71E 99 66 74 83 66E E [100 rows x 4 columns]"}
E )
E _mock_iqufzomgiw = _patcher_yrvzguhxnm.start()
E
E ------------------
E
E File "<ipython-input-4-6c61a9b5d34f>", line 4
E **{"return_value": " 0 1 2 3E ^E SyntaxError: EOL while scanning string literalE E SyntaxError: EOL while scanning string literal (<ipython-input-4-6c61a9b5d34f>, line 4)
Is there plan to support returning objects as it is?
The text was updated successfully, but these errors were encountered:
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:
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?
The text was updated successfully, but these errors were encountered: