0

So I am trying to test an api lookup with my mock data.

I am testing a method within transform.py that imports a module lookup

import lookup

col = lookup.ColFinder()
url = "xyz"
if col.is_present(url):
  do this

lookup.py

import secdata

class ColFinder():
   def is_present(url):
       if url in secdata.CUSTOM_STUFF:
           return secdata.CUSTOM_STUFF[url]

secdata.py

secdata.CUSTOM_STUFF=load("some_file")

I want to mock the JSON (file being loaded within secdata.CUSTOM_STUFF)

I have tried mocking using the unittest.mock with some custom config within tests/resources


CUSTOM_CONFIG = secdata.load(os.path.join(os.path.dirname(__file__),'/resources/custom_config.json'))

import mock
@mock.patch("transform.lookup.secdata.CUSTOM_STUFF" , return_value=CUSTOM_CONFIG)
def test_blah_blah(self, *_):

but this doesn't seem to load the file I am trying to reference. Please can someone help me mock this, point out what am I doing wrong.

Thank you in advance.

Shivangi Singh
  • 1,001
  • 2
  • 11
  • 20
  • you need to mock `ColFinder` and test the various scenarios of that not `secdata` so in this case you would test the logical branches of `is_present` – gold_cy Jul 26 '21 at 17:28
  • @gold_cy Thank you for your response. Yes, I have been able to separately mock the values of is_present, but I am looking for away in which I can just mock load the secdata value in lookup instead of mocking the branches – Shivangi Singh Jul 26 '21 at 17:43
  • but that isn't the correct way to write the unit test, if you want to test `secdata` then test that module by itself. since you're testing `transform.py` you shouldn't be worrying about what is happening in `lookup.py` – gold_cy Jul 26 '21 at 17:44
  • I don't want to test secdata, i just want to mock the whole file as I am writing multiple tests, and dont want to mock separate file. I want to do something like (_load_config_) once – Shivangi Singh Jul 26 '21 at 17:55

0 Answers0