r/pythonhelp • u/Status_Ad6549 • Aug 24 '22
Am I mocking correctly?
In my test I have:
def test_should_convert_csv_headers_to_array(mocker):
expected = [ 'ID', 'Name', 'Image', 'SKU' ]
mock_csv_dict_writer = mocker.MagicMock()
mock_csv_dict_writer.fieldnames.return_value = [ 'ID', 'Name', 'Image', 'SKU' ]
assert expected == headers_to_config_map(mock_csv_dict_writer)All headers_to_config_map does is call fieldnames on csv.DictWriter
I've mocked the csv dictwriter so the return value of calling feildnames returns the array I expect.
I'm just wondering if I'm moving in the right direction in terms of unit testing.
I can't help but think, this doesn't need to be tested because DictWriter.fieldnames() is already tested by the creators of the csv package. My function just wraps that so do I still need to test my function?
3
Upvotes
1
u/Goobyalus Aug 24 '22
You can do
or
to mock out a smaller set of functionality.
Well, this will catch if you messed up the wrapper.
This raises the question of why your wrapper function exists at all - just replace
headers_to_config_map(dictwriter)withdictwriter.fieldnames, right?