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

0

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

How can I test the following in the 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)
    
asked by anonymous 08.10.2018 / 03:45

0 answers