Skip to content

Way to isolate tests from existing environment variables? #4226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
simonvanderveldt opened this issue Oct 24, 2018 · 9 comments
Closed

Way to isolate tests from existing environment variables? #4226

simonvanderveldt opened this issue Oct 24, 2018 · 9 comments
Labels
type: question general question, might be closed after 2 weeks of inactivity

Comments

@simonvanderveldt
Copy link

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.
Is this something that could be added or was this a conscious design decision?

As 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

def pytest_addhooks(pluginmanager):
    os.environ = {}

And 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 #3597
I'd currently do that using:

def pytest_addhooks(pluginmanager):
    os.environ = {"FOO": "bar"}

I 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.

@simonvanderveldt simonvanderveldt changed the title Way to osolate tests from existing environment variables Way to isolate tests from existing environment variables Oct 24, 2018
@simonvanderveldt simonvanderveldt changed the title Way to isolate tests from existing environment variables Way to isolate tests from existing environment variables? Oct 24, 2018
@blueyed
Copy link
Contributor

blueyed commented Oct 24, 2018

JFI: typically CI systems ensure this by design, and tox can also be used for this (where you have to explicitly use passenv).

@nicoddemus nicoddemus added the type: question general question, might be closed after 2 weeks of inactivity label Oct 24, 2018
@nicoddemus
Copy link
Member

IMHO environment isolation is done better by an external tool than adding this to pytest itself, because it is hard to do it correctly from within pytest.

@blueyed
Copy link
Contributor

blueyed commented Oct 24, 2018

Agreeing with @nicoddemus, and closing.

@blueyed blueyed closed this as completed Oct 24, 2018
@simonvanderveldt
Copy link
Author

Hmm, seems rather odd to depend on external tools for basic testing functionality, right?

IMHO environment isolation is done better by an external tool than adding this to pytest itself, because it is hard to do it correctly from within pytest.

From this I assume it's more complex than just setting os.environ to None or {}?
What's so difficult about it? What we do now seems to be working just fine, but there might still be a technical reason I'm not aware of that it's a bad idea.

@RonnyPfannschmidt
Copy link
Member

replacing os.environ is pretty much fundamentally broken and you are lucky if things didn't blow up ^^

@simonvanderveldt
Copy link
Author

@RonnyPfannschmidt Do you have any more info on what exactly is fundamentally broken?

@RonnyPfannschmidt
Copy link
Member

@simonvanderveldt os.environ is a instance of a private mapping class that maps to the underlying c functions that set/get environment variables

shadowing 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

@simonvanderveldt
Copy link
Author

simonvanderveldt commented Oct 29, 2018

@RonnyPfannschmidt Thanks for the info. So far we're doing pure Python only so I guess we should be fine. Will keep it in mind though for possible future situations :)

@RonnyPfannschmidt
Copy link
Member

@simonvanderveldt i strongly suggest to limit this hack as intensely as possible - its a really good starting point for unexpected failures

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants