mymodule.py
def write_df_to_csv(self, df, modified_fn):
new_csv = self.path + "/" + modified_fn
df.to_csv(new_csv, sep=";", encoding='utf-8', index=False)
test_mymodule.py
class TestMyModule(unittest.TestCase):
def setUp(self):
args = parse_args(["-f", "test1"])
self.mm = MyModule(args)
self.mm.path = "Random/path"
self.test_df = pd.DataFrame(
[
["bob", "a"],
["sue", "b"],
["sue", "c"],
["joe", "c"],
["bill", "d"],
["max", "b"],
],
columns=["A", "B"],
)
def test_write_df_to_csv(self):
to_csv_mock = mock.MagicMock()
with mock.patch("project.mymodule.to_csv", to_csv_mock, create=True):
self.mm.write_df_to_csv(self.test_df, "Stuff.csv")
to_csv_mock.assert_called_with(self.mm.path + "/" + "Stuff.csv")
When I run this test, I get:
FileNotFoundError: [Errno 2] No such file or directory: 'Random/path/Stuff.csv'
I'm trying to mock the to_csv in my method. My other tests run as expected, however I'm not sure where I am going wrong with this test. Is my use of MagicMock correct, or am I overlooking something else?