Skip to main content

Pytest plugin to fail a test if it leaves modified `os.environ` afterwards.

Project description

pytest-modified-env

Build Status Python Version wemake-python-styleguide

Pytest plugin to fail a test if it leaves modified os.environ afterwards.

Example:

import os

def test_that_modifies_env() -> None:
    os.environ['CUSTOM_ENV'] = '1'

With pytest-modified-env plugin installed, this test will fail:

___________________________ test_that_modifies_env ____________________________
test_that_modifies_env:4: in pytest_runtest_call
E   RuntimeError: os.environ was changed

Because it adds CUSTOM_ENV inside a test and does not clean it up. In theory it can affect other tests and tests should be isolated!

Installation

pip install pytest-modified-env

Extras

In some cases test still might modify the env in this way. But, it needs an explicit approval for that:

import os
import pytest

@pytest.mark.modify_env()
def test_that_modifies_env() -> None:
    os.environ['CUSTOM_ENV'] = '1'

This test won't fail, eventhough it adds CUSTOM_ENV, because it has modifies_env marker.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-modified-env-0.1.0.tar.gz (3.6 kB view hashes)

Uploaded Source

Built Distribution

pytest_modified_env-0.1.0-py3-none-any.whl (4.1 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page