MobileDynasty / pytest-env

pytest plugin used to set environment variables
MIT License
217 stars 23 forks source link

Example for using pyproject.toml for setting env vars #15

Closed setu4993 closed 3 years ago

setu4993 commented 4 years ago

Thanks for creating and maintaining this package!

Can you please provide an example of using this library with pyproject.toml files that pytest config supports? Neither of the below work. 1:

[tool.pytest.ini_options]
env =[
    "HOME=~/tmp",
    "RUN_ENV=test"
]

2:

[tool.pytest.ini_options]
env ="HOME=~/tmp\nRUN_ENV=test"
nermand commented 3 years ago

The first option works for me

env = [
  "environment=test",
  "api_key=foo"
]
setu4993 commented 3 years ago

@nermand : Interesting. It still doesn't work for me. I'm probably doing something wrong.

pyproject.toml:

...
[tool.pytest.ini_options]
env = [
 "environment=test",
 "api_key=foo"
]

test_env.py:

from os import environ

def test_env_environment():
    assert environ["environment"] == "test"

def test_env_api_key():
    assert environ["api_key"] == "foo"
nermand commented 3 years ago

FWIW I am using python-dotenv for loading .env variables.
Not sure if that affects how envs are parsed

setu4993 commented 3 years ago

No luck even after adding python-dotenv and pytest-dotenv for me :/.

bosatsu commented 3 years ago

@setu4993

The following works for me, and I'm not using python-dotenv:

[tool.pytest.ini_options]
env = '''
    HOME=~/tmp
    RUN_ENV=test
'''
setu4993 commented 3 years ago

Thanks @nermand and @bosatsu! Good pointers. I can confirm that any of the below work without python-dotenv or pytest-dotenv: 1.

[tool.pytest.ini_options]
env = [
 "HOME=~/tmp",
 "RUN_ENV=test"
]
  1. [tool.pytest.ini_options]
    env ="HOME=~/tmp\nRUN_ENV=test"
  2. [tool.pytest.ini_options]
    env = """
    HOME=~/tmp
    RUN_ENV=test
    """

If it doesn't recommend rechecking the pytest version. My pytest version was downgraded to <6.0, without me noticing, and since pyproject.toml support was added in 6.0+, it wasn't working.