I can use python mockito to mock os.environ.get('SOME_VAR')
as demo here.
Main idea
when(os.environ).get(...).thenReturn(MOCKED_VAL)
Though to mock os.environ['SOME_VAR']
syntax, I failed to get it working as this code.
p.s.
My google search results little helpful so I asked here.
The closest I can found is using built-in unittest.path ie patch.dict(SOMEDICT, {'k':'v'}, clear=True)
It seems no answer until today using mockito package.
Here is my solution using builtin unittest mock patch
class TestSomething:
def test_07_env_production_on(self):
"""mock to force ENVIRONMENT to be production"""
from unittest.mock import patch
p = patch.dict(in_dict=os.environ, values={'ENVIRONMENT': 'production'}, clear=False)
p.start()
self.addCleanup(p.stop) # register mockito's unstub() method to unittest's cleanup