Przykład z os.walk(), która zwraca trzy elementową krotkę:
import os def findFile(fileName): for root, dirs, files in os.walk('~/'): if fileName in files: return os.path.join(root, fileName) return NoneTest:
import unittest import mock from mock import call import asdf class TestFindFile(unittest.TestCase): @mock.patch('asdf.os') def test_findFIle(self, mockOs): fileName = 'aaa.txt' mockOs.walk.return_value = [('/home/user', ('folder',), ('bbb.txt',)), ('/home/user/folder', ('fol1', 'fol2'), (fileName,))] mockOs.path.join.return_value = '/home/user/folder' + fileName self.assertEqual('/home/user/folder' + fileName, asdf.findFile(fileName)) mockOs.walk.assert_called_once_with('~/') mockOs.path.join.assert_called_once_with('/home/user/folder', fileName)
Brak komentarzy:
Prześlij komentarz