跳过所有的测试,而不是装饰每次试验的功能与@pytest.标记。skipif()?

0

的问题

我有一个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 =======
pytest python
2021-11-24 03:54:51
1

最好的答案

2

你可以使用的一个装置 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.')

另一种可能性是把测试成一类,并把标记在类相反,正如纳尔逊*卢克的意见。

2021-11-24 12:10:19

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................