i'm having hard time mocking os.environ.get
hoping can help.
i have baseclass imported. i'm trying mock os.environ.get call in base_class file when import baseclass method sub_class module.
project.data.base_class.py
import os class baseclass(object): var1 = os.environ.get('something') var2 = os.environ.get('something') def __init__(self): pass def get(self): return baseclass.var1
project.data.sub_class.py
from project.data.base_class import baseclass class subclass(baseclass): def __init__(self): def run(self): return self.get()
i want test subclass.run
method, when try mocking, can't seem right mock set os.environ. here i've tried:
from mock import patch @patch('base_class.os.environ') def test_sub_class_run(self, mock_base): mock_base.get.side_effect = ['var1', 'var2'] sub_class import sub_class self.assertequal(sub_class.subclass.run(), 'var1') mock import patch
and
@patch('sub_class.base_class.os.environ') def test_sub_class_run(self, mock_base): mock_base.get.side_effect = ['var1', 'var2'] sub_class import sub_class self.assertequal(sub_class.subclass.run(), 'var1')
i feel because of when using os.environ, i'm not mocking properly. i'm not sure appropriate way structure mock mock os call. properly.
that's pseudo code more or less.
i didn't solve problem. did have refactor code , reason because, , think true please correct if i'm wrong!
it looks when try patch library, patching library seems run script or module importing. in case, trying mock os call, os call of thing trying patch @ outermost layer of script. when try patch it, environment variable didn't exist. had move environment variable retrieval class , try patch.
this wrong , misinterpreted did, that's how solved problem.
Comments
Post a Comment