Python mock, patch os.environ returns None -


i have simple module:

# my_module os.path import join os import environ  some_file_name = 'foo.txt'  def file_path():     join(environ['target_directory'], some_file_name) 

and simple test:

import os import unittest os.path import join  mock import patch my_module import some_file_name, file_path   class mytest(unittest.testcase):     def test_can_create_path(self):         some_folder = '/path/to/directory'         patch.dict('my_module.environ', {'target_directory': some_folder}):             self.assertequal(file_path(), join(some_folder, some_file_name))  if __name__ == '__main__':     unittest.main() 

i expected call environ['target_directory'] return '/path/to/directory' returns none

your function returns none, there no explicit return statement. add return:

def file_path():     return join(environ['target_directory'], some_file_name) 

the mock patch worked fine otherwise.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -