6

I want to use the python dotenv-lib at my python project. My dev-environment should use .env-file and the test-suite (pytest) should use .env.test automatically.

Until now I didn't find a satisfying solution.

I'm not very familiar with python. Maybe somebody can point me in the right direction.

Should I load the .env.test file in a pytest hook?

wuarmin
  • 2,569
  • 1
  • 15
  • 27

2 Answers2

13

For anyone coming in 2019 or later:

You can use pytest-dotenv .

jnns
  • 4,450
  • 4
  • 37
  • 70
Lual
  • 2,298
  • 1
  • 22
  • 26
6

After some investigation I came to the conclusion of not using python-dotenv. I ended up using the cool library simple-settings and pytest hooks. It solves my requirements quite well! simple-settings can load settings from several file-types. Which setting-file is loaded can be decided through an environment variable.

Following post can be very helpful, if somebody wants to learn something about pytest and its conftest file: In py.test, what is the use of conftest.py files?

Here's my conftest.py file with two hooks implemented:

import os

def pytest_configure(config):
  os.environ['SIMPLE_SETTINGS'] = 'config.test'
  return config

def pytest_unconfigure(config):
  os.environ['SIMPLE_SETTINGS'] = 'config.development'
  return config

If I run some test with pytest the corresponding environment variable is set. So my test-suite uses automatically the right settings.

wuarmin
  • 2,569
  • 1
  • 15
  • 27