Я пытаюсь написать контрольный пример для моей функции abc (input_file, length, output_file), где функция читает данные из input_file и записывает выходные данные в output_file. Я указываю содержимое моего input_file, и мой output_file записывается с тем же содержимым, что и input_file, после использования mock.open () для записи файла.
Это функция, которую я хочу проверить.
def abc(input_file, length, output_file):
infile = (input_file, 'r')
lines = infile.readlines()
infile.close()
with open(out, "w") as fw:
////do something
outfile.close()
return error
class Testabc(unittest.TestCase):
def setUp(self):
example_file = """ a\t123
b\t456
c\t789"""
output_file_content = """x\t123
y\t531"""
output_file = "sample.tab"
def test_hawkeye_hdrule3(self):
with patch('builtins.open', mock_open(read_data=self.example_file), create=True) as m:
self.assertEqual(abc(m, 13, self.output_file), True)
def test_outfile_creation(self):
with patch('builtins.open', mock_open(read_data=self.example_file), create=True) as m:
self.f = "output.tab"
value = abc(m, 13, self.f)
with open(self.f, "r") as fp:
contents = fp.read()
self.assertEqual(contents, self.output_file_content)
expected result = """x\t123
y\t531"""
which is equal to the output_file_content.
But the data in the contents is
""" a\t123
b\t456
c\t789""" which is the example_file