I have a pytest file which requires environment being set. So I add the following decorator on every function.
JavaScript
x
15
15
1
@pytest.mark.skipif('password' not in os.environ,
2
reason='Environment variable "password" not set.')
3
def test_1(mock):
4
.
5
6
@pytest.mark.skipif('password' not in os.environ,
7
reason='Environment variable "password" not set.')
8
def test_2(mock):
9
.
10
11
@pytest.mark.skipif('password' not in os.environ,
12
reason='Environment variable "password" not set.')
13
def test_3(mock):
14
.
15
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?
JavaScript
1
2
1
====== 25 skipped in 5.96s =======
2
Advertisement
Answer
You can use a fixture with autouse=True
that does the skipping for you:
JavaScript
1
7
1
@pytest.fixture(autouse=True)
2
def skip_if_no_password():
3
if 'password' in os.environ:
4
yield
5
else:
6
pytest.skip('Environment variable "password" not set.')
7
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.