pytest8-skip与xfail

我与影子孤独终老i 提交于 2019-12-02 16:15:39

 

 skip(无条件跳过测试用例)与skipif(有条件跳过测试用例)

 

# test_skip_function.py      函数级别
import pytest
import sys


@pytest.mark.skip(reason='no way of currently testing this')
def test_the_unknown():
    assert 1 == 1


@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.7 or higher")  # 有条件跳过测试用例
def test_function():
    assert 1 == 1

输出结果:D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_function.py
ss
2 skipped in 0.02 seconds

 

# test_skip_class.py   类级别
import pytest
import sys


@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires python3.6 or higher")
class TestSkipClass:
    def test_class(self):
        "requires python3.6 or higher"

输出结果:
D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_class.py
s
1 skipped in 0.02 seconds
# test_skip_module.py   模块级别,跳过整个模块的测试用例
import pytest


pytestmark = pytest.mark.skip(reason='nopass')  
def test_the_unknown():
    assert 1 == 1


def test_module():
    assert 1 == 1

输出结果:
D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_module.py
ss
2 skipped in 0.02 seconds

xfail

# test_xfail.py
import pytest

xfail = pytest.mark.xfail


@xfail
def test_hello():
    assert 0


@xfail(run=False)
def test_hello2():
    assert 0


@xfail("hasattr(os, 'sep')")
def test_hello3():
    assert 0


@xfail(reason="bug 110")
def test_hello4():
    assert 0


@xfail('pytest.__version__[0] != "17"')
def test_hello5():
    assert 0

输出结果:
D:\myproject\pytest_demo>pytest -rx --tb=no test_xfail.py
=================================================================================== test session starts ====================================================================================
platform win32 -- Python 3.6.5, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: D:\myproject\pytest_demo
plugins: allure-pytest-2.8.5
collected 7 items                                                                                                                                                                           

test_xfail.py xxxxxxx                                                                                                                                                                 [100%]

================================================================================= short test summary info ==================================================================================
XFAIL test_xfail.py::test_hello
XFAIL test_xfail.py::test_hello2
  reason: [NOTRUN]
XFAIL test_xfail.py::test_hello3
  condition: hasattr(os, 'sep')
XFAIL test_xfail.py::test_hello4
  bug 110
XFAIL test_xfail.py::test_hello5
  condition: pytest.__version__[0] != "17"
XFAIL test_xfail.py::test_hello6
  reason: reason
XFAIL test_xfail.py::test_hello7
================================================================================ 7 xfailed in 0.11 seconds =================================================================================

skip/xfail with parametrize

# test_skip_xfail_with_parametrize.py
import pytest
import sys


@pytest.mark.parametrize(('n', 'expected'),
                         [(1, 2),
                          pytest.param(1, 0, marks=pytest.mark.xfail),
                          pytest.param(1, 3, marks=pytest.mark.xfail(reason="some bug")),
                          (2, 3),
                          (3, 4),
                          (4, 5),
                          pytest.param(10, 11, marks=pytest.mark.skipif(sys.version_info >= (3, 0), reason="py2k"))])
def test_increment(n, expected):
    assert n + 1 == expected


# 输出结果
D:\myproject\pytest_demo>pytest -qs --tb=no test_skip_xfail_with_parametrize.py
.xx...s
4 passed, 1 skipped, 2 xfailed in 0.05 seconds

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!