Repository: quiqua/pytest-dotenv Branch: master Commit: 2d3b97c3db17 Files: 10 Total size: 11.0 KB Directory structure: gitextract_on98nqwa/ ├── .gitignore ├── LICENSE ├── MANIFEST.in ├── README.md ├── pytest_dotenv/ │ ├── __init__.py │ └── plugin.py ├── setup.cfg ├── setup.py └── tests/ ├── conftest.py └── test_dotenv.py ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] # C extensions *.so # Distribution / packaging .Python env/ build/ develop-eggs/ dist/ downloads/ eggs/ lib/ lib64/ parts/ sdist/ var/ *.egg-info/ .installed.cfg *.egg # PyInstaller # Usually these files are written by a python script from a template # before PyInstaller builds the exe, so as to inject date/other infos into it. *.manifest *.spec # Installer logs pip-log.txt pip-delete-this-directory.txt # Unit test / coverage reports htmlcov/ .tox/ .coverage .cache nosetests.xml coverage.xml # Translations *.mo *.pot # Django stuff: *.log # Sphinx documentation docs/_build/ # PyBuilder target/ ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2020 Marcel Radischat Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: MANIFEST.in ================================================ include LICENSE include README.md recursive-exclude * __pycache__ recursive-exclude * *.py[co] ================================================ FILE: README.md ================================================ # Discontinued This project is not maintained any longer - sorry for any inconvenience this might cause. # 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`: ```sh 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: ```ini [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: ```ini [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`. ================================================ FILE: pytest_dotenv/__init__.py ================================================ __version__='0.5.2' __author__='Marcel Radischat' ================================================ FILE: pytest_dotenv/plugin.py ================================================ # -*- coding: utf-8 -*- from dotenv import load_dotenv, find_dotenv import pytest def pytest_addoption(parser): parser.addini("env_files", type="linelist", help="a line separated list of env files to parse", default=['.env']) parser.addini("env_override_existing_values", type="bool", help="override the existing environment variables", default=False) parser.addoption("--envfile", dest="envfile", default="foo", type=str, help="Overwrite any environment variable specified in this file. This argument ignores the .ini settings.") @pytest.hookimpl(tryfirst=True) def pytest_load_initial_conftests(args, early_config, parser): _override = early_config.getini("env_override_existing_values") for filename in early_config.getini("env_files"): load_dotenv(find_dotenv(filename, usecwd=True), override=_override) 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) ================================================ FILE: setup.cfg ================================================ [metadata] description-file = README.md ================================================ FILE: setup.py ================================================ from setuptools import setup from os import path description = 'A py.test plugin that parses environment files before running tests' # read the contents of the README file this_directory = path.abspath(path.dirname(__file__)) with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: long_description = f.read() setup( name='pytest-dotenv', description=description, long_description=long_description, long_description_content_type='text/markdown', version='0.5.2', author='Marcel Radischat', author_email='marcel@quiqua.eu', url='https://github.com/quiqua/pytest-dotenv', download_url='https://github.com/quiqua/pytest-dotenv/tarball/0.5.2', packages=['pytest_dotenv'], entry_points={'pytest11': ['dotenv = pytest_dotenv.plugin']}, install_requires=['pytest>=5.0.0', 'python-dotenv>=0.9.1'], classifiers=[ 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Natural Language :: English', 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', 'Programming Language :: Python :: 3.3', 'Programming Language :: Python :: 3.4', 'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.6', 'Programming Language :: Python :: 3.7', 'Programming Language :: Python :: 3.8', ] ) ================================================ FILE: tests/conftest.py ================================================ pytest_plugins = 'pytester' ================================================ FILE: tests/test_dotenv.py ================================================ # -*- coding: utf-8 -*- from unittest import mock import pytest @pytest.fixture def mock_os_environ(): with mock.patch.dict("os.environ", clear=True) as m: yield m @pytest.mark.usefixtures("mock_os_environ") def test_ini_file(testdir): testdir.makeini(""" [pytest] env_files = myenv.txt """) testdir.maketxtfile(myenv="FOO=BAR\nSPAM=EGGS") # create a temporary pytest test module testdir.makepyfile(""" import os def test_env_foo(): assert os.environ.get('FOO') == 'BAR' def test_env_spam(): assert os.environ.get('SPAM') == 'EGGS' """) # run pytest with the following cmd args result = testdir.runpytest("-v") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*::test_env_foo PASSED*', '*::test_env_spam PASSED*' ]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 @pytest.mark.usefixtures("mock_os_environ") def test_ini_file_refuse_overwrite(testdir): testdir.makeini(""" [pytest] env_override_existing_values = 0 env_files = myenv.txt overwrite.txt """) testdir.maketxtfile(myenv="FOO=BAR\nSPAM=EGGS") testdir.maketxtfile(overwrite="FOO=EGGS\nSPAM=BAR") # create a temporary pytest test module testdir.makepyfile(""" import os def test_env_foo(): assert os.environ.get('FOO') == 'BAR' def test_env_spam(): assert os.environ.get('SPAM') == 'EGGS' """) # run pytest with the following cmd args result = testdir.runpytest("-v") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*::test_env_foo PASSED*', '*::test_env_spam PASSED*' ]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 @pytest.mark.usefixtures("mock_os_environ") def test_ini_file_allow_overwrite(testdir): testdir.makeini(""" [pytest] env_override_existing_values = 1 env_files = myenv.txt overwrite.txt """) testdir.maketxtfile(myenv="FOO=BAR\nSPAM=EGGS") testdir.maketxtfile(overwrite="FOO=EGGS\nSPAM=BAR") # create a temporary pytest test module testdir.makepyfile(""" import os def test_env_foo(): assert os.environ.get('FOO') == 'EGGS' def test_env_spam(): assert os.environ.get('SPAM') == 'BAR' """) # run pytest with the following cmd args result = testdir.runpytest("-v") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*::test_env_foo PASSED*', '*::test_env_spam PASSED*' ]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 @pytest.mark.usefixtures("mock_os_environ") def test_file_argument_force_overwrite(testdir): testdir.makeini(""" [pytest] env_files = myenv.txt """) testdir.maketxtfile(myenv="FOO=BAR\nSPAM=EGGS") tmp_env_file = testdir.maketxtfile(tmpenv="FOO=BAZ\nBAR=SPAM") # create a temporary pytest test module testdir.makepyfile(""" import os def test_env_foo(): assert os.environ.get('FOO') == 'BAZ' def test_env_spam(): assert os.environ.get('SPAM') == 'EGGS' def test_env_bar(): assert os.environ.get('BAR') == 'SPAM' """) # run pytest with the following cmd args result = testdir.runpytest("-v", "--envfile", str(tmp_env_file)) # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*::test_env_foo PASSED*', '*::test_env_spam PASSED*', '*::test_env_bar PASSED*' ]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0 @pytest.mark.usefixtures("mock_os_environ") def test_env_is_set_before_test_session_is_started(testdir): testdir.makeini(""" [pytest] env_files = myenv.txt """) testdir.maketxtfile(myenv="FOO=BAR") testdir.makeconftest(""" import os assert os.environ.get('FOO') == 'BAR' """) # create a temporary pytest test module testdir.makepyfile(""" import os def test_env_foo(): assert os.environ.get('FOO') == 'BAR' """) # run pytest with the following cmd args result = testdir.runpytest("-v") # fnmatch_lines does an assertion internally result.stdout.fnmatch_lines([ '*::test_env_foo PASSED*', ]) # make sure that that we get a '0' exit code for the testsuite assert result.ret == 0