Git Product home page Git Product logo

pytest-dotenv's Introduction

pytest-dotenv

This little plugin uses python-dotenv to load any environment variables from a .env file. Extra configuration can be defined in any pytest config files, such as pytest.ini, tox.ini and so on.

Installation

Install the plugin with pip:

pip install pytest-dotenv

Basic Usage

If all you want is to load environment variables from a .env file then installing the plugin is all that is needed. python-dotenv will automatically detect your .env file and load it. By default, the plugin won't override any existing system variables.

Non-default configuration

Custom Environment Variable Files

Add a new section named env_files to your pytest config file. You can list as many files as necessary:

[pytest]
env_files =
    .env
    .test.env
    .deploy.env

The files will be loaded and added to the os.environ dict object before any tests are run. If the files are not found on the working directory, it will search for the files in the ancestor directory and upwards.

Overriding Existing Values

By default the plugin will not override any variables already defined in the process' environment. If you want that behavior, you have to use the env_override_existing_values setting:

[pytest]
env_override_existing_values = 1
env_files =
    .env
    .test.env
    .deploy.env

Alternative: Specify the file at the command line

You also have the option to run your tests with py.test --envfile path/to/.env. This will load all defined environment variables and overwrite any existing ones regardless of the configuration env_override_existing_values.

pytest-dotenv's People

Contributors

frob avatar jaehnigen-kp avatar kianmeng avatar martinhinrichs avatar mateuszczubak avatar mpessas avatar vladimir-iliev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pytest-dotenv's Issues

Environment variables are not found when pytest is started in shell

Hi,
I am using Pycharm 2019.3.1, the package works great when I start pytest by using right-click on the respective test folder from within Pycharm, all environment variables are found and used. However, if I start pytest from the console, the variables are not found. Am I missing a cli parameter that tells pytest to use pytest-dotenv?

Add tests

Please add, and include them in the source tarball, so they can be used by rpm to validate the package.

env_override_existing_values doesn't work if --envfile option is set

"override" is hardcoded by True. Suggest to add _override = early_config.getini("env_override_existing_values") to make this configurable

def pytest_sessionstart(session):
    config = session.config
    if config.getoption("envfile", default=None) is not None:
        load_dotenv(dotenv_path=config.getoption("envfile"), override=True)

Special casing `PYTHONPATH` in pytest-dotenv?

When using pytest to test utilities (CLI) or a mix of API and utility which require setting PYTHONPATH, the current set of affairs is not great:

  • because pytest_dotenv updates the envvars inside the running process, it's not taken in account by sys.path
  • since 7.0, pytest has added a pythonpath configuration entry but that only updates sys.path, so subprocesses don't take that into account

So if one wants to avoid having to set the envvar externally, one has to hit their head on that wall until they figure that both is necessary.

Having special support for PYTHONPATH was understandably rejected in python-dotenv (theskumar/python-dotenv#36) but I figure pytest-dotenv is a lot more specific, and having a coherent PYTHONPATH & sys.path in test contexts seems a lot more sensible in the context of pytest-ing.

NB: I also opened an issue to see if the Pytest folks would be willing to update the pythonpath feature such that it'd also update the envvar: pytest-dev/pytest#10067

Listing multiple .env files should source them sequentially even when env_override_existing_values is not set

Hey there! I'm using this plugin and I've noticed the following behavior, which is somewhat confusing. If I have my pytest-dotenv config as follows:

env_files =
    .env  <-- defines FOOBAR=5
    .env.test  <-- defines FOOBAR=7

then I end up with FOOBAR=5.
I understand this can be fixed with env_override_existing_values=1, but the docs seem to suggest that this is intended for overriding existing env vars in the current process, rather than env vars in a previously listed file. In my opinion, it would make sense to overwrite values from previously specified env files by default, while leaving env_override_existing_values=1 for deciding whether or not existing env vars in the process should be overwritten.

I'd be happy to open a PR if the proposed behavior makes sense.

How to get PyCharm test run working with pytest-dotenv?

I succeeded with running my test with pytest-dotenv from command line as below quote

============================= test session starts ==============================
platform linux -- Python 3.6.9, pytest-5.2.2, py-1.8.0, pluggy-0.13.0
rootdir: /my/local/path/to/pytest-start, inifile: pytest.ini
plugins: forked-1.1.3, dotenv-0.4.0, xdist-1.30.0
collected 1 item

tests/test_dotenv.py .

============================== 1 passed in 0.01s ===============================

Though when running the test with PyCharm test run mode, the plugin not working.
So my question is how to get PyCharm test run working with pytest-dotenv?

Breaks manage.py in Django projects using django-dotenv

For Django projects using django-dotenv and calling dotenv.read_dotenv() from manage.py, installing this library will break manage.py due to a namespace conflict between django-dotenv and python-dotenv (a dependency that this library will install). I know this isn't a Django-specific library and that it would be outside of scope to try to resolve this issue, but might at least be worth a warning in the README.

Add ability to pass as argument

It would be nice to be able to pass --env-file or something similar as a CLI option. I switch stuff like databases off .env files and would like to avoid maintaining a 1-to-1 of pytest.ini files and .env files. Avoids pytest-dev.ini -> dev.env, pytest-qa.ini -> qa.env and pytest-prod.ini -> prod.env.

Use `.env` as default?

This looks like a great little plug-in that will save me from having to remember to prefix my pytest/tox commands with dotenv-run to load my .env file for testing. Would you be amenable to using .env as the default, so the plugin can be used without additional configuration? It's the default used in python-dotenv itself and seems to be largely the default for most things I've seen using this sort of thing.

How to use pytest-dotenv only on integration test?

Hello

pytest-dotenv is very interesting for loading dotenv for integration tests (ie test that depends on the environment), for example to simulate the test environments in CI.

But this approach shall not be used for unit test that shall mock/stub/patch every dependency to outside, and typically the environment variable shall be simulated by something like monkeypatch.setenv. This is the I in FIRST principles (https://medium.com/@tasdikrahman/f-i-r-s-t-principles-of-testing-1a497acda8d6)

How we can restrict pytest-dotenv to only tests marked as "integration_tests"?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.