project\
src\
__init__.py
tests\
__init__.py
stubs\r_json_response_expired.json
在我的测试中,我必须加载一个 JSON 文件。这个。
@patch('requests.post')
def test_subscription_delete_only_if_invalid(self, mock_post):
with app.app_context():
expires_at = datetime.utcnow()
m = self.inject_expired_r_json()
def inject_expired_r_json(self):
with open('stubs/r_json_response_expired.json') as fb_file:
receipt_response = json.load(fb_file)
m = MagicMock()
m.json = MagicMock(name='json')
m.json.return_value = receipt_response
return m
运行测试 VS Code 时显示:
FileNotFoundError:[Errno 2] 没有这样的文件或目录:'stubs/r_json_response_expired.json'
我.env的项目中有:
PYTHONPATH=./src
在 settings.json (VS CODE)
"terminal.integrated.env.osx": {
"PYTHONPATH": "${workspaceFolder}/src"
},
"python.envFile": "${workspaceFolder}/.env"
如果我碰巧切换了 IDE,我不喜欢更改测试中的路径以避免破坏 PyCharm。