【发布时间】:2020-06-19 22:49:56
【问题描述】:
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。
【问题讨论】:
-
运行
pip install -e .在 virtualenv 中以开发模式安装您的项目,然后在中心位置使用DIRNAME = os.path.dirname(__file__)并将所有路径挂起。
标签: python visual-studio-code configuration