pytest-dotenv-modern


Namepytest-dotenv-modern JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryA modern pytest plugin that loads environment variables from dotenv files
upload_time2025-09-14 12:22:04
maintainerNone
docs_urlNone
authorNone
requires_python>=3.11
licenseMIT
keywords pytest dotenv environment testing
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # pytest-dotenv-modern

A modern pytest plugin that loads environment variables from dotenv files before running tests.

This project is a maintained replacement for the original `pytest-dotenv` plugin, built with modern Python practices and full support for contemporary pytest versions.

## Features

- 🚀 **Modern**: Built with Python 3.8+ and latest pytest versions
- 🔧 **Flexible**: Load from multiple dotenv files
- ⚙️ **Configurable**: Configure via pytest.ini, tox.ini, or command line
- 🔄 **Override Control**: Choose whether to override existing environment variables
- 🔍 **Smart Discovery**: Automatically finds .env files in current and parent directories
- 📝 **Well Tested**: Comprehensive test suite
- 🏗️ **Type Hinted**: Full type hints for better IDE support

## Installation

Install using uv (recommended):

```bash
uv add pytest-dotenv-modern
```

Or using pip:

```bash
pip install pytest-dotenv-modern
```

## Quick Start

1. Create a `.env` file in your project root:
```bash
# .env
DATABASE_URL=sqlite:///test.db
API_KEY=your-test-api-key
DEBUG=True
```

2. Run your tests:
```bash
pytest
```

The plugin will automatically discover and load your `.env` file!

## Configuration

### Automatic Discovery

By default, the plugin will look for a `.env` file in the current directory and parent directories. If found, it will be loaded automatically.

### Configuration File

Add configuration to your `pytest.ini`, `tox.ini`, or `setup.cfg`:

```ini
[pytest]
env_files = 
    .env
    .env.test
    .env.local

env_override_existing_values = 1
```

### Command Line

Use command line options for one-off overrides:

```bash
# Load specific env file
pytest --envfile .env.production

# Load multiple files
pytest --envfile .env --envfile .env.test

# Override existing environment variables
pytest --override-existing-vars
```

## Configuration Options

### `env_files`

Specify one or more dotenv files to load:

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

Files are loaded in the order specified. The plugin searches for files in the current directory and parent directories.

### `env_override_existing_values`

Control whether to override existing environment variables:

```ini
[pytest]
env_override_existing_values = 1  # Override existing vars
# env_override_existing_values = 0  # Don't override (default)
```

### Command Line Options

- `--envfile PATH`: Load environment variables from specified file(s)
- `--override-existing-vars`: Override existing environment variables

## Examples

### Basic Usage

```python
# test_example.py
import os

def test_environment_variables():
    # These variables are loaded from your .env file
    assert os.getenv("DATABASE_URL") == "sqlite:///test.db"
    assert os.getenv("API_KEY") == "your-test-api-key"
    assert os.getenv("DEBUG") == "True"
```

### Multiple Environment Files

```ini
# pytest.ini
[pytest]
env_files = 
    .env           # Base configuration
    .env.test      # Test-specific overrides
    .env.local     # Local developer settings
```

### Environment-Specific Testing

```bash
# Test with development environment
pytest --envfile .env.development

# Test with staging environment  
pytest --envfile .env.staging

# Test with production environment
pytest --envfile .env.production
```

## Advanced Usage

### Programmatic Access

You can access the plugin instance from your tests:

```python
import pytest
from pytest_dotenv_modern import get_dotenv_plugin

def test_plugin_info():
    plugin = get_dotenv_plugin()
    if plugin:
        loaded_files = plugin.loaded_files
        print(f"Loaded {len(loaded_files)} dotenv files")
```

### Custom Configuration

```python
# conftest.py
import pytest
from pytest_dotenv_modern.plugin import DotenvPlugin

@pytest.fixture(scope="session")
def setup_custom_env(request):
    """Load additional environment files for specific test runs."""
    plugin = DotenvPlugin(request.config)
    
    # Load additional file based on test marker
    if request.node.get_closest_marker("integration"):
        plugin.load_file(".env.integration", override=True)
    
    return plugin
```

## Development

### Setup Development Environment

```bash
# Clone the repository
git clone https://github.com/jobissjo/pytest-dotenv-modern.git
cd pytest-dotenv-modern

# Install with uv
uv sync --dev

# Or with pip
pip install -e ".[dev]"
```

### Running Tests

```bash
# Run tests with uv
uv run pytest

# Run tests with coverage
uv run pytest --cov=pytest_dotenv --cov-report=html

# Run with different Python versions using tox
tox
```

### Code Quality

```bash
# Format code
uv run black src tests
uv run isort src tests

# Lint code
uv run flake8 src tests
uv run mypy src

# Run pre-commit hooks
uv run pre-commit run --all-files
```

## Migration from pytest-dotenv

This plugin is designed as a drop-in replacement for the original `pytest-dotenv`. 

### Breaking Changes

- Minimum Python version is 3.8+
- Package name changed to `pytest-dotenv-modern`
- Some internal APIs may have changed

### Migration Steps

1. Uninstall the old plugin:
   ```bash
   pip uninstall pytest-dotenv
   ```

2. Install the new plugin:
   ```bash
   uv add pytest-dotenv-modern
   ```

3. Update any imports (if you were importing from the plugin):
   ```python
   # Old
   from pytest_dotenv import ...
   
   # New  
   from pytest_dotenv_modern import ...  # Same import path!
   ```

4. Your existing configuration should work without changes!

## Contributing

Contributions are welcome! Please read our contributing guidelines and submit pull requests to our GitHub repository.

## License

MIT License - see LICENSE file for details.

## Changelog

### 0.1.0

- Initial release
- Modern Python 3.8+ support
- Full pytest compatibility
- Comprehensive test suite

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "pytest-dotenv-modern",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.11",
    "maintainer_email": null,
    "keywords": "pytest, dotenv, environment, testing",
    "author": null,
    "author_email": "Your Name <your.email@example.com>",
    "download_url": "https://files.pythonhosted.org/packages/a7/7d/3f4cf2ee186981843794f133c6ff582357f88b581c66f0307e8a282e4724/pytest_dotenv_modern-0.2.0.tar.gz",
    "platform": null,
    "description": "# pytest-dotenv-modern\r\n\r\nA modern pytest plugin that loads environment variables from dotenv files before running tests.\r\n\r\nThis project is a maintained replacement for the original `pytest-dotenv` plugin, built with modern Python practices and full support for contemporary pytest versions.\r\n\r\n## Features\r\n\r\n- \ud83d\ude80 **Modern**: Built with Python 3.8+ and latest pytest versions\r\n- \ud83d\udd27 **Flexible**: Load from multiple dotenv files\r\n- \u2699\ufe0f **Configurable**: Configure via pytest.ini, tox.ini, or command line\r\n- \ud83d\udd04 **Override Control**: Choose whether to override existing environment variables\r\n- \ud83d\udd0d **Smart Discovery**: Automatically finds .env files in current and parent directories\r\n- \ud83d\udcdd **Well Tested**: Comprehensive test suite\r\n- \ud83c\udfd7\ufe0f **Type Hinted**: Full type hints for better IDE support\r\n\r\n## Installation\r\n\r\nInstall using uv (recommended):\r\n\r\n```bash\r\nuv add pytest-dotenv-modern\r\n```\r\n\r\nOr using pip:\r\n\r\n```bash\r\npip install pytest-dotenv-modern\r\n```\r\n\r\n## Quick Start\r\n\r\n1. Create a `.env` file in your project root:\r\n```bash\r\n# .env\r\nDATABASE_URL=sqlite:///test.db\r\nAPI_KEY=your-test-api-key\r\nDEBUG=True\r\n```\r\n\r\n2. Run your tests:\r\n```bash\r\npytest\r\n```\r\n\r\nThe plugin will automatically discover and load your `.env` file!\r\n\r\n## Configuration\r\n\r\n### Automatic Discovery\r\n\r\nBy default, the plugin will look for a `.env` file in the current directory and parent directories. If found, it will be loaded automatically.\r\n\r\n### Configuration File\r\n\r\nAdd configuration to your `pytest.ini`, `tox.ini`, or `setup.cfg`:\r\n\r\n```ini\r\n[pytest]\r\nenv_files = \r\n    .env\r\n    .env.test\r\n    .env.local\r\n\r\nenv_override_existing_values = 1\r\n```\r\n\r\n### Command Line\r\n\r\nUse command line options for one-off overrides:\r\n\r\n```bash\r\n# Load specific env file\r\npytest --envfile .env.production\r\n\r\n# Load multiple files\r\npytest --envfile .env --envfile .env.test\r\n\r\n# Override existing environment variables\r\npytest --override-existing-vars\r\n```\r\n\r\n## Configuration Options\r\n\r\n### `env_files`\r\n\r\nSpecify one or more dotenv files to load:\r\n\r\n```ini\r\n[pytest]\r\nenv_files =\r\n    .env\r\n    .env.test\r\n    .env.deploy\r\n```\r\n\r\nFiles are loaded in the order specified. The plugin searches for files in the current directory and parent directories.\r\n\r\n### `env_override_existing_values`\r\n\r\nControl whether to override existing environment variables:\r\n\r\n```ini\r\n[pytest]\r\nenv_override_existing_values = 1  # Override existing vars\r\n# env_override_existing_values = 0  # Don't override (default)\r\n```\r\n\r\n### Command Line Options\r\n\r\n- `--envfile PATH`: Load environment variables from specified file(s)\r\n- `--override-existing-vars`: Override existing environment variables\r\n\r\n## Examples\r\n\r\n### Basic Usage\r\n\r\n```python\r\n# test_example.py\r\nimport os\r\n\r\ndef test_environment_variables():\r\n    # These variables are loaded from your .env file\r\n    assert os.getenv(\"DATABASE_URL\") == \"sqlite:///test.db\"\r\n    assert os.getenv(\"API_KEY\") == \"your-test-api-key\"\r\n    assert os.getenv(\"DEBUG\") == \"True\"\r\n```\r\n\r\n### Multiple Environment Files\r\n\r\n```ini\r\n# pytest.ini\r\n[pytest]\r\nenv_files = \r\n    .env           # Base configuration\r\n    .env.test      # Test-specific overrides\r\n    .env.local     # Local developer settings\r\n```\r\n\r\n### Environment-Specific Testing\r\n\r\n```bash\r\n# Test with development environment\r\npytest --envfile .env.development\r\n\r\n# Test with staging environment  \r\npytest --envfile .env.staging\r\n\r\n# Test with production environment\r\npytest --envfile .env.production\r\n```\r\n\r\n## Advanced Usage\r\n\r\n### Programmatic Access\r\n\r\nYou can access the plugin instance from your tests:\r\n\r\n```python\r\nimport pytest\r\nfrom pytest_dotenv_modern import get_dotenv_plugin\r\n\r\ndef test_plugin_info():\r\n    plugin = get_dotenv_plugin()\r\n    if plugin:\r\n        loaded_files = plugin.loaded_files\r\n        print(f\"Loaded {len(loaded_files)} dotenv files\")\r\n```\r\n\r\n### Custom Configuration\r\n\r\n```python\r\n# conftest.py\r\nimport pytest\r\nfrom pytest_dotenv_modern.plugin import DotenvPlugin\r\n\r\n@pytest.fixture(scope=\"session\")\r\ndef setup_custom_env(request):\r\n    \"\"\"Load additional environment files for specific test runs.\"\"\"\r\n    plugin = DotenvPlugin(request.config)\r\n    \r\n    # Load additional file based on test marker\r\n    if request.node.get_closest_marker(\"integration\"):\r\n        plugin.load_file(\".env.integration\", override=True)\r\n    \r\n    return plugin\r\n```\r\n\r\n## Development\r\n\r\n### Setup Development Environment\r\n\r\n```bash\r\n# Clone the repository\r\ngit clone https://github.com/jobissjo/pytest-dotenv-modern.git\r\ncd pytest-dotenv-modern\r\n\r\n# Install with uv\r\nuv sync --dev\r\n\r\n# Or with pip\r\npip install -e \".[dev]\"\r\n```\r\n\r\n### Running Tests\r\n\r\n```bash\r\n# Run tests with uv\r\nuv run pytest\r\n\r\n# Run tests with coverage\r\nuv run pytest --cov=pytest_dotenv --cov-report=html\r\n\r\n# Run with different Python versions using tox\r\ntox\r\n```\r\n\r\n### Code Quality\r\n\r\n```bash\r\n# Format code\r\nuv run black src tests\r\nuv run isort src tests\r\n\r\n# Lint code\r\nuv run flake8 src tests\r\nuv run mypy src\r\n\r\n# Run pre-commit hooks\r\nuv run pre-commit run --all-files\r\n```\r\n\r\n## Migration from pytest-dotenv\r\n\r\nThis plugin is designed as a drop-in replacement for the original `pytest-dotenv`. \r\n\r\n### Breaking Changes\r\n\r\n- Minimum Python version is 3.8+\r\n- Package name changed to `pytest-dotenv-modern`\r\n- Some internal APIs may have changed\r\n\r\n### Migration Steps\r\n\r\n1. Uninstall the old plugin:\r\n   ```bash\r\n   pip uninstall pytest-dotenv\r\n   ```\r\n\r\n2. Install the new plugin:\r\n   ```bash\r\n   uv add pytest-dotenv-modern\r\n   ```\r\n\r\n3. Update any imports (if you were importing from the plugin):\r\n   ```python\r\n   # Old\r\n   from pytest_dotenv import ...\r\n   \r\n   # New  \r\n   from pytest_dotenv_modern import ...  # Same import path!\r\n   ```\r\n\r\n4. Your existing configuration should work without changes!\r\n\r\n## Contributing\r\n\r\nContributions are welcome! Please read our contributing guidelines and submit pull requests to our GitHub repository.\r\n\r\n## License\r\n\r\nMIT License - see LICENSE file for details.\r\n\r\n## Changelog\r\n\r\n### 0.1.0\r\n\r\n- Initial release\r\n- Modern Python 3.8+ support\r\n- Full pytest compatibility\r\n- Comprehensive test suite\r\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A modern pytest plugin that loads environment variables from dotenv files",
    "version": "0.2.0",
    "project_urls": {
        "Homepage": "https://github.com/jobissjo/pytest-dotenv-modern",
        "Issues": "https://github.com/jobissjo/pytest-dotenv-modern/issues",
        "Repository": "https://github.com/jobissjo/pytest-dotenv-modern"
    },
    "split_keywords": [
        "pytest",
        " dotenv",
        " environment",
        " testing"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "0056193bb765db6a47da3337abf2b12843f0eab8a0260b6807a256e5d4dec513",
                "md5": "247f4e6166da3dc503d8cad01788ef3b",
                "sha256": "bba89a8dd7def290c93e11bd364e77ded93835ef2027cd77261056178b3f52d0"
            },
            "downloads": -1,
            "filename": "pytest_dotenv_modern-0.2.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "247f4e6166da3dc503d8cad01788ef3b",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.11",
            "size": 6322,
            "upload_time": "2025-09-14T12:21:59",
            "upload_time_iso_8601": "2025-09-14T12:21:59.846357Z",
            "url": "https://files.pythonhosted.org/packages/00/56/193bb765db6a47da3337abf2b12843f0eab8a0260b6807a256e5d4dec513/pytest_dotenv_modern-0.2.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "a77d3f4cf2ee186981843794f133c6ff582357f88b581c66f0307e8a282e4724",
                "md5": "0414661caa3020f0fcdb3d7d8c0093b8",
                "sha256": "3b21ab9824892101aac2a44add25ee3631e7e00875e7744b3c550bbf54d0b002"
            },
            "downloads": -1,
            "filename": "pytest_dotenv_modern-0.2.0.tar.gz",
            "has_sig": false,
            "md5_digest": "0414661caa3020f0fcdb3d7d8c0093b8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.11",
            "size": 34579,
            "upload_time": "2025-09-14T12:22:04",
            "upload_time_iso_8601": "2025-09-14T12:22:04.822525Z",
            "url": "https://files.pythonhosted.org/packages/a7/7d/3f4cf2ee186981843794f133c6ff582357f88b581c66f0307e8a282e4724/pytest_dotenv_modern-0.2.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-09-14 12:22:04",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jobissjo",
    "github_project": "pytest-dotenv-modern",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "pytest-dotenv-modern"
}
        
Elapsed time: 2.20205s