防止所有Python Unitests基于环境变量运行

发布于 2025-01-26 00:40:06 字数 176 浏览 1 评论 0原文

如果某个环境变量未设置为特定值,我是否可以防止所有单元测试运行?

例如,我希望仅在os.getenv(db_url)返回sqlite:///:memory:时才能运行测试。

我想在全球配置此此类,因此我不需要查看每个测试类 /设置功能即可单独检查此功能。

Is there a way I could prevent all unit tests from running if a certain environment variable is not set to a specific value?

For example I want tests to only run if os.getenv(DB_URL) returns sqlite:///:memory:.

I'd like to configure this globally so I don't need to review every single test class / setup function to check this individually.

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

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

发布评论

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

评论(1

橘香 2025-02-02 00:40:06

有很多方法可以实现这一目标,但是固定装置可能是最简单的

import os
import pytest

DB_URL = "foo"

@pytest.fixture(autouse=True, scope="session")
def verify_db_url():
    expected_url = "sqlite:///:memory:"
    if os.getenv(DB_URL) != expected_url:
        pytest.exit("Exiting due to incorrect database environment variable.")
    

There's many ways to accomplish this, but a fixture is probably the easiest

import os
import pytest

DB_URL = "foo"

@pytest.fixture(autouse=True, scope="session")
def verify_db_url():
    expected_url = "sqlite:///:memory:"
    if os.getenv(DB_URL) != expected_url:
        pytest.exit("Exiting due to incorrect database environment variable.")
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文