[Unable to overwrite a variable during unit testing]

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.

As working with a software testing company and facing a similar kind of issue found that you overwriting the file as desired but when you call the test you are not using the mocked file.
Wrapping the function seemed to work:

instead of this line of code:- self.assertEqual(mainscript.print_me(),‘End This Issue’)
Please try This:-
self.assertEqual(mainscript.print_me(mainscript.load_json(mainscript.org_file)),‘End This Issue’)

Here you can find the test file code:-
#test_mainscript.py
import json
import mainscript
import unittest
from unittest.mock import patch

class Testmainscript(unittest.TestCase):
@patch(‘mainscript.org_file’,‘unitfile2.json’)
def test_print_me(self):
self.assertEqual(mainscript.org_file, ‘unitfile2.json’)
self.assertEqual(mainscript.print_me(mainscript.load_json(mainscript.org_file)),‘End This Issue’)

if name == ‘main’:
unittest.main()