如何在pytest中添加env变量

问题描述 投票:0回答:0

我正在尝试为pytest添加env变量。我已经成功地为常规的python脚本添加了env和配置,但我无法为pytest使用envars。

 somefile.env 
 KEY = "12345"

 Config.py
 import os
 headers = {"Key": os.environ.get("KEY"), "content_Type": "application/json"}

以上是.env和配置文件中给出的值。我可以在python脚本中访问头值,但我无法在pytest中访问这个头值。例如,这是我在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中调用headers时,我得到的键值是None。

python-3.x pycharm environment-variables pytest
© www.soinside.com 2019 - 2024. All rights reserved.