擅长:python、mysql、java
<p>Pytest有skip和skipif修饰符,类似于Python单元测试模块(它使用<code>skip</code>和<code>skipIf</code>),可以在文档<a href="https://docs.pytest.org/en/latest/how-to/skipping.html#skipping-test-functions" rel="noreferrer">here</a>中找到</p>
<p>链接中的示例可在此处找到:</p>
<pre><code>@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():
...
</code></pre>
<p>第一个示例总是跳过测试,第二个示例允许您有条件地跳过测试(当测试依赖于平台、可执行版本或可选库时非常好)</p>
<p>例如,如果我想检查是否有人为测试安装了库pandas</p>
<pre><code>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():
...
</code></pre>