1

It's common practice to use VSCode's launch.json to set environment variables for running and debugging python. However if I use VSCode's "Testing" view for python testing, is there a way for the pytest or unittest scripts to use one of the configurations in launch.json to set env vars? If not, what would be the best way to set them for VSCode's Testing?

PyTest has pytest-env, however my hope was that VSCode would have a consistent way to handle env vars with python, one that would not require configuration duplication or bespoke code that ties the two.

As I'm using pytest, I ended up defining the env vars in pytest.ini as suggested here, however as before it creates duplication with launch.json

Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

1 Answers1

1

I think, you want use your env vars as pytest global vars. You can assign them in the conftest.py:

def pytest_configure(config):
    pytest.var = os.environ.get("FOO")

Advantage of this approach is that you can not only use this variable within the test case, but pass it to the decorators and other fixtures:

@pytest.mark.skipif(pytest.var == "foo")
def test_000():
    ...
Leonid-98
  • 71
  • 6