Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add env variable to pytest

I am trying to add env variables to pytest. I have successfully managed to add env and configure for regular python scripts but i am unable to use env vars for pytest.

 somefile.env 
 KEY = "12345"

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

Above are the values given in .env and config file. I can access headers value in python script, but i am unable to access this header value inside pytest. Eg this is my code in 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

When i call headers in pytest i am getting key value as None

like image 574
user29496 Avatar asked Apr 08 '26 04:04

user29496


1 Answers

The solution here is as described by @user29496

# contents of pytest.ini
[pytest]
env =
    D:RUN_ENV=1234

This will silently fail/not work unless you install pytest-env

pip install pytest-env

At this point, the RUN_ENV should be correctly passed. See also How to pass environment variables to pytest

like image 69
jandom Avatar answered Apr 10 '26 01:04

jandom



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!