0

Trying to figure out how to overwrite a variable during unit testing:

#mainscript.py
import json

org_file = 'unitfile.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, 'unitfile2.json') # Correctly overwrites file
        self.assertEqual(mainscript.print_me(),'Bye World') #does not pass, still reading unitfile.json instead of unitfile2.json
if __name__ == '__main__':
    unittest.main()

This test should fail because because I'm overwriting org_file with unitestfile2.json (which contains {'text':'Bye World'}) instead of unittestfile.json (which contains {'text':'Hello World'})

But its currently passing because the variable isn't being overwritten

2 Answers 2

1

What you are looking for is called "mocking", an example for your case:

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.print_me(),'Hello World')

if __name__ == '__main__':
    unittest.main()
Sign up to request clarification or add additional context in comments.

8 Comments

I'm still getting a passing result are you sure its mocking unitfile with unitfile2?
Not sure, It's how this is done but might need some changes, You can add a test to see if it does change: self.assertEqual(mainscript.org_file, 'unitfile2.json')
I added the test AssertionError: <Mock id='140009482389680'> != 'unitfile2.json' . Im pretty sure its not mocking correctly
Try this: @patch('mainscript.org_file', 'unitfile2.json')
I see, you need to make with open(... a function and call it in print_me to get the content, because your code now load the text before it get patched
|
0

I found the issue is that I am overwriting the file as expected but when I call the test I am not using the mocked file. Wrapping the function seemed to work:

#mainscript.py
import json


org_file = 'unitfile.json'
def load_json (json_file):
    with open ('{0}'.format(org_file)) as f:
       json_object =  json.load(f)
    return json_object


def print_me(df_file):
    return (df_file['text'])

def main():
    print_me()

if __name__ == '__main__':
    main()

Here I used the wrapped patch as an input

#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)),'Bye World')

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

Test now work:

----------------------------------------------------------------------
Ran 1 test in 0.000s

OK

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.