Unable to mock function called from another files in python using python-mockito

82 Views Asked by At

I am using python-mockito library for mocking. Here's a sample code piece that I am working on.

build_config.py

from read_report_config import *

def config_builder(*some parameters*):
   .
   .
   .
   new_enabled_list = read_gsheet_config(logging, "Some sheet name", config_dir, data)
   .
   .
   .
   return config_data;

test_build_config.py

from unittest import TestCase
from mockito import when, mock, verifyStubbedInvocationsAreUsed
import requests
from src import build_config, read_report_config

class SimpleMockitoTest(TestCase):

    def test_build_config(self):
        new_enabled_list = ['hello', 'something', 'it', 'is']
        when(read_report_config).read_gsheet_config(...).thenReturn(new_enabled_list)
        config_data = build_config.config_builder(*some parameters*)


if __name__ == '__main__':
    unittest.main()

Now the problem is that, when I try to test test_build_config, it doesn't mock the read_gsheet_config, it actually runs it, I don't know why?

1

There are 1 best solutions below

0
Ujjawal Pandey On BEST ANSWER

It's important to understand where to patch. As answered in this stackoverflow answer
I have to patch it like:

when(build_config).read_gsheet_config(...)thenReturn()