from unittest.mock import MagicMock, mock_open, patch
mo = mock_open()
def mywrite(b):
assert type(b) == bytearray or type(b) == bytes
assert b == b'data'
return len(b)
mo.return_value.write = mywrite
with patch("builtins.open", mo) as mock_file:
with open("a.mp3", "wb") as f:
total_size = f.write(b"data")
print("MP3 file size: ", total_size)
print(mock_file.mock_calls) # !!!***