我正在尝试为gateway.py
设置一些单元测试。当我在gateway.py(debugging)内部一次调用以下测试代码时,它似乎并没有模拟出os.environ调用
os.environ
environ({'PATH': '.....'})
os.environ['x']
Traceback (most recent call last):
File "/home/justin/.IdeaIC2019.3/config/plugins/python-ce/helpers/pydev/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
exec(exp, global_vars, local_vars)
File "<input>", line 1, in <module>
File "/usr/lib/python3.6/os.py", line 669, in __getitem__
raise KeyError(key) from None
KeyError: 'x'
测试文件:
import unittest
from unittest.mock import patch
import mock
from httmock import urlmatch, HTTMock
import requests
class TestGateway(unittest.TestCase):
@patch('gateway.os.environ', mock.MagicMock(return_value="mock_env_var"))
def test_zoho_event(self, mock_os):
mock_os.environ.assert_called()
我的目录结构:
.
├── docker
│ ├── flask
│ │ ├── Dockerfile
│ │ └── prod_vars.env
│ └── nginx
│ ├── x.gateway.com.au.conf
│ ├── Dockerfile
│ └── nginx.conf
├── docker-compose.yml
├── gateway.ini
├── gateway.py
├── Jenkinsfile
├── logs
│ └── gateway.log
├── __pycache__
│ ├── gateway.cpython-36.pyc
│ ├── test_gateway.cpython-36.pyc
│ └── wsgi.cpython-36.pyc
├── requirements.txt
├── test_gateway.py
└── wsgi.py
我已经多次重读https://docs.python.org/3/library/unittest.mock.html#where-to-patch,而且我确定我正在修补从gateway.py内部加载函数的位置。不确定我错过了什么?