使用os.environ.getenv而不是ini或env文件中的pytest中的访问环境变量

发布于 2025-02-07 16:41:09 字数 584 浏览 2 评论 0原文

如果这有点漫不经心,请提前道歉,但是在过去的几个小时中,我很筋疲力尽。

我目前正在尝试使用Pytest Explorer以及带有Pytest配置的Visual Studio Code Test Explorer。我使用一个单独的脚本将环境变量设置在终端中的内部,该脚本从Amazon参数存储中提取秘密,并将它们存储在我的终端中作为环境变量。

但是,pytest无法“看到”我的脚本设置的环境变量,而包含环境变量的变量只是返回非类型。

如果我不使用pytest,则可以轻松地拾取环境变量,因为我只是呼叫os.environ.get,并且可以很好地检索环境变量。

我已经看到了多种方法来设置PYTEST内部的环境变量,但是它们通常涉及使用.INI文件或.env文件,该文件需要将环境变量进行硬编码到这些文件中。

有没有办法说“嘿,pytest,我知道您不喜欢os.environ.get,但我保证这些环境变量将在运行时存在。”我以这种方式表达它,因为它就像Pytest在运行时无法看到系统中存在的任何环境变量,并且必须手动告诉该变量存在于.ENV文件中(您必须在其中使用硬码敏感信息)。

对此的任何帮助将不胜感激。

Apologies in advance if this is a bit rambly, but I am exhausted from trying to figure this out for the past few hours.

I am currently trying to use PyTest along with the Visual Studio Code Test Explorer with a PyTest configuration. I am setting my environment variables inside of my terminal using a separate script that pulls secrets from Amazon Parameter Store and stores them inside of my terminal as an environment variable.

However, PyTest is unable to "see" my environment variables as set by my script and my variables that contain my environment variables are just returning NoneType.

If I do not use PyTest, the environment variables are picked up easily as I just make calls to os.environ.get, and it works perfectly to retrieve the environment variables.

I have seen a variety of ways to set environment variables inside of PyTest, but they usually involve using an .ini file or a .env file which requires hardcoding the values of the environment variables into those files.

Is there a way to say "Hey PyTest, I know that you don't like os.environ.get, but I promise that those environment variables will exist at runtime." I phrase it this way because it's like PyTest cannot see any environment variables that exist in the system at runtime and must be manually told that the variables exist in a .env file (where you have to hardcode sensitive information).

Any help with this would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆凄凉。 2025-02-14 16:41:09

PYTest与环境变量合作,很好。考虑这个简单的测试:

import os


def test_env():
    assert os.environ.get("MYVAR") == "expected_value"

如果在我的环境中运行pytest没有myvar,我会得到:

$ pytest
...
test_envvar.py F                                                                                [100%]

=============================================== FAILURES ================================================
_______________________________________________ test_env ________________________________________________
test_envvar.py:5: in test_env
    assert os.environ.get("MYVAR") == "expected_value"
E   AssertionError: assert None == 'expected_value'
...

但是,如果我在pytest的环境中设置 runnig 测试通过:

$ export MYVAR=expected_value
$ pytest
...
test_envvar.py .                                                                                [100%]

=========================================== 1 passed in 0.01s ===========================================

如果您没有看到环境变量的预期值,这表明您没有将它们设置在正确的位置。

pytest works with environment variables just fine. Consider this simple test:

import os


def test_env():
    assert os.environ.get("MYVAR") == "expected_value"

If run pytest without MYVAR in my environment, I get:

$ pytest
...
test_envvar.py F                                                                                [100%]

=============================================== FAILURES ================================================
_______________________________________________ test_env ________________________________________________
test_envvar.py:5: in test_env
    assert os.environ.get("MYVAR") == "expected_value"
E   AssertionError: assert None == 'expected_value'
...

But if I set it in the environment in which pytest is runnig, the test passes:

$ export MYVAR=expected_value
$ pytest
...
test_envvar.py .                                                                                [100%]

=========================================== 1 passed in 0.01s ===========================================

If you're not seeing the expected values for your environment variables, that suggests you're not setting them in the right place.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文