on-internet
← Community

Pytest: Patch a global variable

Stack Overflow ·

How do you patch a variable with mock or pytest-mock. Assume the variable is defined in another python script and is used by many other scripts. I would like to mock it within pytest_cmdline_main so that all the scripts using that variable will be mocked accordingly. A quick example will be in env.py VAR = "something" in conftest.py import os import sys from unittest.mock import patch TEST_DIR = os.path.dirname(os.path.realpath(__file__)) class MockThings(): def __init__(self): self.setup() def setup(self): mock_var = patch('env.VAR').start() mock_var.return_value = "updated" def pytest_cmdlin

Comments

Sign in or sign up to comment and upvote.

No comments yet.