Pytest has the skip and skipif decorators, similar to the Python unittest module (which uses skip
and skipIf
), which can be found in the documentation here.
Examples from the link can be found here:
@pytest.mark.skip(reason="no way of currently testing this")
def test_the_unknown():
...
import sys
@pytest.mark.skipif(sys.version_info < (3,3),
reason="requires python3.3")
def test_function():
...
The first example always skips the test, the second example allows you to conditionally skip tests (great when tests depend on the platform, executable version, or optional libraries.
For example, if I want to check if someone has the library pandas installed for a test.
import sys
try:
import pandas as pd
except ImportError:
pass
@pytest.mark.skipif('pandas' not in sys.modules,
reason="requires the Pandas library")
def test_pandas_function():
...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…