I've built my pytest class in the following manner:
class Test_Exit_Code(object):
def setup_class(cls):
If 2==2:
# stop execution of the test class
def test_a(self):
assert True==True
As you can see inside the setup_class method i've inserted an IF , what i want to achieve in the if section is that if a certain condition appears to be true instead of the comment i want to return exit code 5 , i.e. no tests were collected . so far i haven't found any way to do it.
You could try calling pytest.exit()
. Also it seems you are missing the classmethod
decorator
import pytest
class Test_Exit_Code:
@classmethod
def setup_class(cls):
if 2==2:
pytest.exit("test exit code setup failed", returncode=5)
def test_this_wont_run(self):
assert True
def test_neither_does_this(self):
assert False
Output:
>python -m pytest test.py
====================== test session starts ======================
platform win32 -- Python 3.9.12, pytest-7.1.1, pluggy-1.0.0
rootdir: /example
plugins: anyio-3.5.0
collected 2 items
test.py
===================== no tests ran in 0.25s =====================
!!!!!! _pytest.outcomes.Exit: test exit code setup failed !!!!!!!