ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

pytest8-skip与xfail

2019-10-28 18:00:10  阅读:232  来源: 互联网

标签:pytest8 skip py xfail pytest test def


 

 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

 

标签:pytest8,skip,py,xfail,pytest,test,def
来源: https://www.cnblogs.com/wang-mengmeng/p/11753982.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有