-1

I have the following test function:

def test_env_var(self):
    with mock.patch.dict(os.environ, {"env_var": "value_for_env_var"}):
        self.assertEqual(tested_function(), "expected_value_if_env_var_exists")
        os.environ.pop("env_var")
        self.assertEqual(tested_function(), "expected_value_if_env_var_doesnt_exists")

If I execute it the test pass. This test belongs to a module that belongs to a package with a lot of modules containing tests.
Every test with a test function that uses the dictionary os.environ['key'] fails.
It seems like the mocking of os.environ affects other tests. I'm using parallel testing with pytest.
How is that possible?

frankfalse
  • 1,553
  • 1
  • 4
  • 17
amit
  • 71
  • 2
  • 10
  • 1
    please share the logs of the failed tests. it's good to know why it happened but will not enough – Nguyen Apr 11 '23 at 09:22

1 Answers1

-1

I have created a file with 2 functions under test. The file with this functions is called env_function.py and is in the same folder of the test file (test_os_environment.py).

Below there is the code of env_function.py:

import os

def tested_function():
    return os.environ

def tested_function_2():
    if os.environ['LANGUAGE'] == 'en_US':
        return 0
    return 1

The tested_function_2() use the value of the key LANGUAGE in os.environ (the key LANGUAGE is really present in my system) as you say in the question:

Every test with a test function that uses the dictionary os.environ['key'], fails

The test file test_os_environment.py contains 2 tests function (I have modified your test function):

import unittest
from unittest import mock
import os
from env_function import tested_function, tested_function_2

class MyTestCase(unittest.TestCase):

    def test_env_var(self):
        with mock.patch.dict("os.environ", {"env_var": "value_for_env_var"}):
            self.assertTrue("env_var" in tested_function())
            os.environ.pop("env_var")
            self.assertTrue("env_var" not in tested_function())
        self.assertTrue("env_var" not in os.environ)

    def test_func2(self):
        self.assertEqual(0, tested_function_2())
        self.assertTrue("LANGUAGE" in os.environ)

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

After the context manager with mock.patch.dict(), the dictionary os.environ doesn't contain the env_var key. This is proofed by the assertion:

self.assertTrue("env_var" not in os.environ)

The tests contained in the function test_func2() pass (tested_function_2() return 0 as expected).

This is the output of the test execution with pytest in parallel:

$ pytest -n 2
========================= test session starts =========================
platform linux -- Python 3.6.9, pytest-7.0.1, pluggy-1.0.0
rootdir: /home/frank/tests
plugins: mock-3.6.1, xdist-3.0.2
gw0 [4] / gw1 [4]
....                                                            [100%]
========================== 4 passed in 0.40s ==========================

To find your problem I suggest you to compare my context with yours.


This other post could be useful.

frankfalse
  • 1,553
  • 1
  • 4
  • 17
  • It's not obvious that this would address whatever problem OP is observing. – chepner Apr 11 '23 at 12:59
  • The test function is in other file.I tried to add clear flag, the same problem. – amit Apr 12 '23 at 05:40
  • @amit I have modify the answer, give me a feedback please. – frankfalse Apr 12 '23 at 06:53
  • this isn't solved my problem. – amit Apr 12 '23 at 07:25
  • @amit I know that my answer can't solve your problem but I'm asking you to add in your question or here in the comments other details to show the differences between my context and your context, otherwise this question can't reach a satisfactory answer. – frankfalse Apr 12 '23 at 07:31