0

I have the following python file board.py:

 def __init__(self, language):
        self.foo = Foo(language)
        self.words = Aux(self.foo)

And I'm creating this test_file:

    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.board = Board('pt')

    def test_total_time(self):
        self.board.total_time(True)
        #some assert


But I'm getting a FileNotFoundError because Aux.___init____() calls a self.foo.method() that opens a file and reads from it. Is there a way to mock self.foo.method(), or the class Aux?

Enorio
  • 65
  • 8

1 Answers1

0

You will want to patch the module. If you give me the name of the test file and the class you are testing. I can finish this answer for you.

In the test file:

import unittest

def BoardTestCase(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        super().setUpClass()
        cls.aux_mock = unittest.mock.patch('file_undertest.Aux')
        cls.board = Board('pt')

    def test_total_time(self):
        self.board.total_time(True)
        #some assert

I would suggest using pytest instead of the standard library unittest. Your tests will be written as functions meaning you can reuse the Board class only when needed to. You can set more robust fixtures (Board class test cases) and the mocker extension is more intuitive if you spend the 15 minutes to wrap your head around it.

Daniel Butler
  • 3,239
  • 2
  • 24
  • 37