Python unit testing issue with mock patch dict os env variable

Viewed 28
@mock.patch.dict(os.environ, {"CONTAINER": "TrUe"}, clear=True)
def test_use_container_true(self):
    """
    Test the env var for if a containers should be used or not.
    """
    self.assertEqual(a.__container__, True, "not returning python bool True")

@mock.patch.dict(os.environ, {"CONTAINER": "fAlSE"}, clear=True)
def test_use_container_false(self):
    """
    Test the env var for if a containers should be used or not.
    """
    self.assertEqual(a.__container__, False, "not returning python bool False")

Here is the code it is testing:

#!/usr/bin/env python3

env_container = os.getenv('CONTAINER')
if env_container is None:
    __container__ = False
else:
    env_container = env_container.lower()
    if env_container == 'true':
        __container__ = True
    else:
        __container__ = False

I get this error:

F......
======================================================================
FAIL: test_use_container_false (test_amadla.TestGlobalVars)
Test the env var for if a containers should be used or not.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.9/unittest/mock.py", line 1768, in _inner
    return f(*args, **kw)
  File "/home/user/Projects/SiteNetSoft/Amadla/AmadlaToolServerImageBuilder/test_amadla.py", line 57, in test_use_container_false
    self.assertEqual(a.__container__, False, "not returning python bool False")
AssertionError: True != False : not returning python bool False

----------------------------------------------------------------------
Ran 10 tests in 0.002s

FAILED (failures=1)

It seems it keeps the value True.

Is it taking the value from mock as it is supposed to do?

0 Answers
Related