【发布时间】:2020-10-07 12:29:49
【问题描述】:
我正在尝试将环境变量添加到 pytest。我已经成功地为常规 python 脚本添加了 env 和配置,但我无法将 env vars 用于 pytest。
somefile.env
KEY = "12345"
Config.py
import os
headers = {"Key": os.environ.get("KEY"), "content_Type": "application/json"}
以上是 .env 和配置文件中给出的值。我可以在 python 脚本中访问 headers 值,但我无法在 pytest 中访问这个 header 值。例如,这是我在 pytest 中的代码
import os
from config import headers
print(headers) #---> as this works as expected and prints key value
#Pytests starts
def test_one():
print(headers) # prints key as None
当我在 pytest 中调用标头时,我将键值设为 None
【问题讨论】:
-
您能否更新您的问题并添加您尝试过的代码?我的意思是你可以通过 Python 中的
os模块访问环境变量:os.environ.get("CUSTOM_ENV_VARIABLE")那似乎是什么问题? -
更新问题
-
我认为您可以将它们添加到
pytest.ini文件中。 -
试过但没有运气。在 pytest.ini [pytest] env = HOME=~/tmp RUN_ENV=1234 中输入以下代码,因为1234 是我的关键。我不确定我是否以正确的方式这样做
-
这能回答你的问题吗? How to pass environment variables to pytest
标签: python-3.x pycharm environment-variables pytest