file.py:
JavaScript
x
7
1
GLOB_VAR = []
2
3
def my_func(x, msg):
4
if x:
5
logging.warning(msg)
6
GLOB_VAR.append(x)
7
test.py:
JavaScript
1
10
10
1
@patch('file.GLOB_VAR', [])
2
@patch('logging.Logger.warning')
3
def test_file_with_msg(self, logging):
4
x = 'test_x'
5
msg = 'test_msg'
6
7
my_func(x=x, msg=msg)
8
logging.assert_called_with(message)
9
assert x in GLOB_VAR
10
I always get an AssertionError. from the line assert x in GLOB_VAR
Let me say that I DO need a global variable
Advertisement
Answer
It turned out that I shouldn’t have patched the global variable as I need to assert against the file’s global variable and not against the a mock’s instance global variable.
This does does the trick:
test.py
JavaScript
1
11
11
1
from file import my_func, GLOB_VAR
2
3
@patch('logging.Logger.warning')
4
def test_file_with_msg(self, logging):
5
x = 'test_x'
6
msg = 'test_msg'
7
8
my_func(x=x, msg=msg)
9
logging.assert_called_with(message)
10
assert x in GLOB_VAR
11