pytest-dotenv icon indicating copy to clipboard operation
pytest-dotenv copied to clipboard

pytest-dotenv with pytest instead of py.test does not allow cli args

Open tmh-azinhal opened this issue 4 years ago • 4 comments

If pytest is used pytest-dotenv will only load an environment file from the pytest.ini. If py.test is used then the cli arg --envfile will be used.

pytest --envfile <path to .env> does nothing and will fallback to the config given in pytest.ini

tmh-azinhal avatar Nov 03 '20 12:11 tmh-azinhal

I have tested this and for me

  • pytest --envfile=my.env ignores my.env file and uses .env instead.
  • py.test works the same way, no differences.
$ pip freeze | grep pytest-dot
pytest-dotenv==0.5.2

belkka avatar Nov 05 '21 02:11 belkka

Looks like this is because .env is loaded by default if env_files is not specified in pytest.ini. And --envfile=my.env looks for me like being ignored because I am testing django. env_files from ini file are loaded before django configuration which goes before handling command line option --envfile. That's why django settings don't see values set via --envfile. Similar problem was discussed in https://github.com/MobileDynasty/pytest-env/issues/3

belkka avatar Nov 05 '21 14:11 belkka

Thanks! I can try to look into this.

Do you by chance have a minimal setup example that I can try @belkka ?

quiqua avatar Nov 05 '21 15:11 quiqua

I can confirm that this still doesn't work. Specifying the .env.local file in the pyproject.toml works just fine, but not via the command line. I have no minimal setup for you, but you can execute something like this: python -m pytest -p vscode_pytest --collect-only --envfile .env.local tests

major-mayer avatar Nov 23 '23 07:11 major-mayer