[英]Testing function taking multiple files
我有一個函數,它接受 3 個文件並返回一個包含文件數據的元組。 例如:
第一個文件:
SVF2018-05-24_12:02:58.917
NHR2018-05-24_12:02:49.914
第二個文件:
SVF2018-05-24_1:04:11.332
NHR2018-05-24_1:04:02.979
第三個文件:
SVF_塞巴斯蒂安·維特爾_法拉利
NHR_Nico Hulkenberg_RENAULT
我得到這樣的結果:
result = (
[('SVF', '2018-05-24_12:02:58.917'), ('NHR', '2018-05-24_12:02:49.914')],
[('SVF', '2018-05-24_1:04:11.332'), ('NHR', '2018-05-24_1:04:02.979')],
[['SVF', 'Sebastian Vettel', 'FERRARI'], ['NHR', 'Nico Hulkenberg', 'RENAULT']]
)
函數本身看起來像這樣:
def read_files(start_log, end_log, abbr) -> tuple:
"""
Takes two .log files - start and end, and a file containing abbreviation explanations.
Processes the data from the files and returns a tuple of lists containing lines for each file.
"""
for argument in [start_log, end_log, abbr]:
if not os.path.isfile(argument):
raise FileNotFoundError('Attribute must be a file.')
with open(argument, 'r') as f:
if argument == abbr:
abbr_lines = [line.strip().split('_') for line in f]
elif argument == start_log:
start_lines = [(line[:3], line.strip()[3:]) for line in f]
start_lines.pop()
else:
end_lines = [(line[:3], line.strip()[3:]) for line in f]
return start_lines, end_lines, abbr_lines
我需要為它編寫一個測試。
我發現錯誤沒有問題:
class ReadFilesTestCase(unittest.TestCase):
def test_file_not_found_error(self):
with self.assertRaises(FileNotFoundError):
read_files('a.txt', 'b.txt', 'c.txt')
但是我真的很難將幾個文件作為函數的參數來模擬。
我一直在嘗試這樣做:
class ReadFilesTestCase(unittest.TestCase):
def setUp(self):
self.file_1 = mock.patch("builtins.open", mock.mock_open(read_data=self.file_1_data))
self.file_2 = mock.patch("builtins.open", mock.mock_open(read_data=self.file_1_data))
self.file_3 = mock.patch("builtins.open", mock.mock_open(read_data=self.file_1_data))
def test_read_files:
self.assertEqual(read_files(self.file_1, self.file_2, self.file_3), self.result)
但我有一個 FileNotFoundError。 我也試過@mock.patch.multiple - 效果不佳。
我想知道是否可以模擬文件,所以我只是這樣寫:
self.assertEqual(read_files(fake_file_1, fake_file_2, fake_file_3), self.result)
我應該使用什么技術? 我很感激任何建議。
這里的問題是,使用mock_open
您只能對所有文件名進行假open
,而不能對特定文件名進行假open
。 我在這里看到兩種可能性:滾動你自己的open
open_mock
而不是使用open_mock
使用side_effect
(有點難看,並且與實現緊密耦合),或者使用類似pyfakefs
東西來模擬文件系統(不那么難看,但重量級) )。
第一種方法可能如下所示:
class ReadFilesTestCase(unittest.TestCase):
@mock.patch("os.path.isfile", return_value=True)
def test_read_files(self, mocked_isfile):
# as we have to mock the for look in the open file,
# we need to provide the lines as arrays (this will probably go in setUp)
file1_lines = ["SVF2018-05-24_12:02:58.917",
"NHR2018-05-24_12:02:49.914"]
file2_lines = ["SVF2018-05-24_1:04:11.332",
"NHR2018-05-24_1:04:02.979"]
file3_lines = ["SVF_Sebastian Vettel_FERRARI",
"NHR_Nico Hulkenberg_RENAULT"]
open_mock = MagicMock(name='open', spec=open)
# we have to first mock __enter__ to account for context manager
# then we use side_effect to define the result subsequent calls to open()
open_mock.return_value.__enter__.side_effect = [
file1_lines, file2_lines, file3_lines
]
with mock.patch('builtins.open', open_mock):
# the file names don't matter in this case
assert read_files("f1", "f2", "f3") == expected_result
對於第二種方法,您需要pyfakefs :
from pyfakefs import fake_filesystem_unittest
class ReadFilesTestCase(fake_filesystem_unittest.TestCase):
def setUp(self):
self.setUpPyfakefs()
def test_read_files(self, mocked_isfile):
# we create the needed files in the fake filesystem
self.fs.create_file("a.txt",
contents="SVF2018-05-24_12:02:58.917\nNHR2018-05-24_12:02:49.914")
self.fs.create_file("b.txt",
contents="SVF2018-05-24_1:04:11.332\nNHR2018-05-24_1:04:02.979")
self.fs.create_file("c.txt",
contents="SVF_Sebastian Vettel_FERRARI\nNHR_Nico Hulkenberg_RENAULT")
# now the files exist in the fake filesystem (e.g. in memory), and you can just use them
assert read_files("a.txt", "b.txt", "c.txt") == expected_result
_Disclaimer_: I'm a contributor to pyfakefs.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.