Home > database >  Unittest - mock os.listdir is not used in testfunction / returns wrong value
Unittest - mock os.listdir is not used in testfunction / returns wrong value

Time:12-13

I want to test the following function, but still struggle with a best practice with testing I/O operations.

def get_weight_file(path: Union[Path, str]) -> str:
    """Finds weights (.model) file in a directory

    Parameters
    ----------
    path: Union[Path, str]
        Path where to find the weights file

    Returns
    -------
    str
        Filename of the weights (.model) file

    """
    only_files = [
        file for file in os.listdir(path) if os.path.isfile(os.path.join(path, file))
    ]
    model_file = [file for file in only_files if file.endswith(".model")]

    if len(model_file) == 0:
        raise FileNotFoundError("No weights file found in current directory")
    if len(model_file) > 1:
        raise MultipleFilesError("Please provide a single weights file")

    return model_file[0]

I tried to mock os.listdir.

@mock.patch("os.listdir", return_value=["test.model", "test.txt", "text.yaml"])
def test_get_weight_file(listdir):
    assert get_weight_file(path="./") == "test.model"

This is the error:

        if len(model_file) == 0:
>           raise FileNotFoundError("No weights file found in current directory")
E           FileNotFoundError: No weights file found in current directory

It seems the function was not able to retreive the "test.model" file. Anyway, it does not work and I don´t know why and I also doubt my way to solve this is best practice. Can anyone hell me how to tackle this?

CodePudding user response:

Actually, you should patch @mock.patch('os.listdir'), see where-to-patch.

Besides, I also patch os.path.isfile() method, because I don't have these directory files on my real file system, I mock the return value of it to True.

E.g.

get_weight_file.py:

import os


def get_weight_file(path):
    only_files = [
        file for file in os.listdir(path) if os.path.isfile(os.path.join(path, file))
    ]
    print('os.listdir(path): ', os.listdir(path))
    model_file = [file for file in only_files if file.endswith(".model")]
    return model_file[0]

test_get_weight_file.py:

from unittest import TestCase, mock
import unittest
from get_weight_file import get_weight_file


class TestGetWeightFile(TestCase):
    @mock.patch("os.path.isfile", return_value=True)
    @mock.patch("os.listdir", return_value=["test.model", "test.txt", "text.yaml"])
    def test_get_weight_file(self, mock_listdir, mock_isfile):
        print('mock_isfile: ', mock_isfile.return_value)
        print('mock_listdir: ', mock_listdir.return_value)
        assert get_weight_file(path="./") == "test.model"


if __name__ == '__main__':
    unittest.main()

test result:

mock_isfile:  True
mock_listdir:  ['test.model', 'test.txt', 'text.yaml']
os.listdir(path):  ['test.model', 'test.txt', 'text.yaml']
.
----------------------------------------------------------------------
Ran 1 test in 0.001s

OK
  • Related