{"action":"created","issue":{"url":"https://api.github.com/repos/pytest-dev/pytest/issues/4226","repository_url":"https://api.github.com/repos/pytest-dev/pytest","labels_url":"https://api.github.com/repos/pytest-dev/pytest/issues/4226/labels{/name}","comments_url":"https://api.github.com/repos/pytest-dev/pytest/issues/4226/comments","events_url":"https://api.github.com/repos/pytest-dev/pytest/issues/4226/events","html_url":"https://github.com/pytest-dev/pytest/issues/4226","id":373567265,"node_id":"MDU6SXNzdWUzNzM1NjcyNjU=","number":4226,"title":"Way to isolate tests from existing environment variables?","user":{"login":"simonvanderveldt","id":204286,"node_id":"MDQ6VXNlcjIwNDI4Ng==","avatar_url":"https://avatars0.githubusercontent.com/u/204286?v=4","gravatar_id":"","url":"https://api.github.com/users/simonvanderveldt","html_url":"https://github.com/simonvanderveldt","followers_url":"https://api.github.com/users/simonvanderveldt/followers","following_url":"https://api.github.com/users/simonvanderveldt/following{/other_user}","gists_url":"https://api.github.com/users/simonvanderveldt/gists{/gist_id}","starred_url":"https://api.github.com/users/simonvanderveldt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/simonvanderveldt/subscriptions","organizations_url":"https://api.github.com/users/simonvanderveldt/orgs","repos_url":"https://api.github.com/users/simonvanderveldt/repos","events_url":"https://api.github.com/users/simonvanderveldt/events{/privacy}","received_events_url":"https://api.github.com/users/simonvanderveldt/received_events","type":"User","site_admin":false},"labels":[{"id":224849198,"node_id":"MDU6TGFiZWwyMjQ4NDkxOTg=","url":"https://api.github.com/repos/pytest-dev/pytest/labels/type:%20question","name":"type: question","color":"cc317c","default":false}],"state":"closed","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2018-10-24T16:23:34Z","updated_at":"2018-10-25T10:08:34Z","closed_at":"2018-10-24T21:30:40Z","author_association":"NONE","body":"It could be I've missed something but I've done quiet a bit of searching and it seems there's no out of the box/default way of isolating tests from the environment variables from the calling environment.\r\nIs this something that could be added or was this a conscious design decision?\r\n\r\nAs a workaround I currently do this in `conftest.py`, which works, though I'm not sure how much I'm abusing the system by doing this :P\r\n```\r\ndef pytest_addhooks(pluginmanager):\r\n    os.environ = {}\r\n```\r\n\r\nAnd as an extension of this it would be nice if there would be a way to have this isolation and allow one to globally override/set environment variables before discovery/importing happens, to allow easy testing of modules that make use of environment variables at module level. This sounds similar to https://github.com/pytest-dev/pytest/issues/3597\r\nI'd currently do that using:\r\n```\r\ndef pytest_addhooks(pluginmanager):\r\n    os.environ = {\"FOO\": \"bar\"}\r\n```\r\nI guess a more specific hook one could add to `conftests.py` that runs before module discovery/import might be one way to enable something like this."},"comment":{"url":"https://api.github.com/repos/pytest-dev/pytest/issues/comments/432993722","html_url":"https://github.com/pytest-dev/pytest/issues/4226#issuecomment-432993722","issue_url":"https://api.github.com/repos/pytest-dev/pytest/issues/4226","id":432993722,"node_id":"MDEyOklzc3VlQ29tbWVudDQzMjk5MzcyMg==","user":{"login":"RonnyPfannschmidt","id":156838,"node_id":"MDQ6VXNlcjE1NjgzOA==","avatar_url":"https://avatars1.githubusercontent.com/u/156838?v=4","gravatar_id":"","url":"https://api.github.com/users/RonnyPfannschmidt","html_url":"https://github.com/RonnyPfannschmidt","followers_url":"https://api.github.com/users/RonnyPfannschmidt/followers","following_url":"https://api.github.com/users/RonnyPfannschmidt/following{/other_user}","gists_url":"https://api.github.com/users/RonnyPfannschmidt/gists{/gist_id}","starred_url":"https://api.github.com/users/RonnyPfannschmidt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/RonnyPfannschmidt/subscriptions","organizations_url":"https://api.github.com/users/RonnyPfannschmidt/orgs","repos_url":"https://api.github.com/users/RonnyPfannschmidt/repos","events_url":"https://api.github.com/users/RonnyPfannschmidt/events{/privacy}","received_events_url":"https://api.github.com/users/RonnyPfannschmidt/received_events","type":"User","site_admin":false},"created_at":"2018-10-25T10:08:34Z","updated_at":"2018-10-25T10:08:34Z","author_association":"MEMBER","body":"@simonvanderveldt `os.environ` is a instance of a private mapping class that maps to the underlying c functions that set/get environment variables\r\n\r\nshadowing it with random python data structures just hides away access to the actual data that will still affect subprocesses and c libraries and in addition it breaks expectations for quite a few things out there"}}
{"actor":{"display_login":"RonnyPfannschmidt"}}