Skip to content
Discussion options

You must be logged in to vote

Hi @Tim-Kirkwood

The file is being created by this line:

with open(outpath, "w") as csv_file:

not by:

pd.Series(entry_names).to_csv(...)

Opening a path with mode "w" creates the file if it doesn't exist (and truncates it if it does).

So even if you successfully patch:

pd.Series.to_csv

this still happens:

with open(outpath, "w") as csv_file:
    # file already exists at this point

If your goal is to test that to_csv() is called correctly, then the existence of the empty file is expected.

For example:

mock_to_csv.assert_called_once()

can verify the pandas interaction independently.

If you want a test with no filesystem interaction at all, mock open() as well:

from unittest.mock import mock_…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@Tim-Kirkwood
Comment options

Answer selected by Tim-Kirkwood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants