How to perform unit test with unittest.mock for reading and writing files?

Asked

Viewed 94 times

0

Good Night!!! This time my doubt is about unit tests. Come on.

How can I test the section below in unittest.mock environment?

def save_html_file(conteudo, filename, encoding='iso8859-1'):
    assert isinstance(conteudo, str), '"conteudo" deve ser do tipo str'
    soup = BeautifulSoup(conteudo, 'html5lib')
    output_path = os.path.dirname(filename)
    os.makedirs(output_path, 0o777, True)
    with open(filename, 'wb') as file_out:
        file_out.write(soup.prettify(encoding=encoding))

import unittest

from unittest import mock
class TestMock(TestCase):
    @patch('os.path.isdir')
    @patch('os.makedirs')
    def test_save_html_file2(self, patch_makedirs, patch_isdir):
        patch_isdir.return_value = False
        legis.save_html_file(self.soup2.prettify(), '/tmp/teste/arquivos/file.html')
        self.assertTrue(patch_makedirs.called)
  • Testar o que ? Se está salvando o arquivo HTML ?

  • Test everything! directory creation, file creation, content reading, coding of the generated file. But all in mock environment.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.