Skip to content
Advertisement

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

I have a pytest file which requires environment being set. So I add the following decorator on every function.

@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):
    ....

Is it a way to skip all the test instead of decorating each test function?

BTW, it just skip the tests with the following message. Is it a way to display a warning information of missing environment variable?

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

Advertisement

Answer

You can use a fixture with autouse=True that does the skipping for you:

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

Another possibility is to put the tests into a class and put the marker on the class instead, as mentioned by Luke Nelson in the comments.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement