Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

python - How do I disable a test using pytest?

Let's say I have a bunch of tests:

def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...

Is there a decorator or something similar that I could add to the functions to prevent pytest from running just that test? The result might look something like...

@pytest.disable()
def test_func_one():
    ...

def test_func_two():
    ...

def test_func_three():
    ...
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...