I am using the below code:
#mainscript.py
import json
org_file = ‘resourceFile.json’
def load_json (file_name):
with open (’{0}’.format(org_file)) as f:
json_object = json.load(f)
return json_object
new_file = load_json(org_file)
def print_me():
return (new_file[‘text’])
def main():
print_me()
if name == ‘main’:
main()
Unit test:
#test_mainscript.py
import json
import mainscript
import unittest
from unittest.mock import patch
class Testmainscript(unittest.TestCase):
# def setUp(self):
# Not needed when using mock
@patch(‘mainscript.org_file’,‘unitfile2.json’)
def test_print_me(self):
self.assertEqual(mainscript.org_file, ‘resourcefile2.json’) # Correctly overwrites file
self.assertEqual(mainscript.print_me(),‘End This Issue’) #does not pass, still reading resourceFile.json instead of resourcefile2.json
if name == ‘main’:
unittest.main()
This test needs to be failed because I’m overwriting org_file with resourcefile2.json (which contains {‘text’:‘End This Issue’}) instead of resourceFile.json (which contains {‘text’:‘End This Game’})
But it’s still passing because the variable isn’t being overwritten.