python - How to change return value of mocked obj? -
i have class with
import pandas pd class foo(object): def __init__(self): self.info = pd.dataframe() def getdata(self): self.__readcsv() def __readcsv(self): self.info = pd.read_csv(self.filename)
i have unit test class with
class test(unittest.testcase): def test(self): mock = patch('foo.pandas.read_csv') foo().getdata() ...
how can change pd.read_csv(self.filename)
return value dataframe({'column1': series([1., 2., 3.]),'column2': series([4., 5., 6.])})
test whether self.info
assigned, assertequal
?
are using python 3 ? python 3 has built in mock library, python 2.x need use third party mock library (http://www.voidspace.org.uk/python/mock/index.html).
to mock pd.read_csv
can use following
# python 3.x unittest.mock import patch # python 2.x # mock import patch import pandas pd class test(unittest.testcase): @patch('foo.pd.read_csv') def test(self, mock_read_csv): mock_read_csv.return_value = pd.dataframe({'column1': series([1., 2., 3.]),'column2': series([4., 5., 6.])}) assert foo().getdata()
Comments
Post a Comment