Skip to content

Way to isolate tests from existing environment variables? #4226

Closed
@simonvanderveldt

Description

@simonvanderveldt

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: questiongeneral question, might be closed after 2 weeks of inactivity

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions