跳过所有测试而不是用@pytest.mark.skipif() 装饰每个测试函数?

Skip all the test instead of decorating each test function with @pytest.mark.skipif()?

我有一个需要设置环境的 pytest 文件。所以我在每个函数上添加了以下装饰器。

@pytest.mark.skipif('password' not in os.environ,
                    reason='Environment variable "password" not set.')
def test_1(mock):
    ....

@pytest.mark.skipif('password' not in os.environ,
                    reason='Environment variable "password" not set.')
def test_2(mock):
    ....

@pytest.mark.skipif('password' not in os.environ,
                    reason='Environment variable "password" not set.')
def test_3(mock):
    ....

是否可以跳过所有测试而不是装饰每个测试函数?

顺便说一句,它只是跳过带有以下消息的测试。有没有办法显示缺少环境变量的警告信息?

====== 25 skipped in 5.96s =======

您可以使用带有 autouse=True 的夹具来为您跳过:

@pytest.fixture(autouse=True)
def skip_if_no_password():
    if 'password' in os.environ:
        yield
    else:
        pytest.skip('Environment variable "password" not set.')

另一种可能性是将测试放入 class 并将标记放在 class 上,正如 Luke Nelson 在评论中提到的那样。