pytest学习--参数和skip

import pytest
import sys
import requests


# pytest.mark.skip 跳过用例
"""
    pytest.mark.skip(*,reason = None )
    :param	reason(str)跳过测试功能的原因
"""


@pytest.mark.skip(reason='there is no way')
def test_the_unknown():
    print("这条测试用例将会被跳过")
    assert 1


def test_the_known():
    print("这条测试用例将不会被跳过")
    assert 1


# skipif 有条件地跳过某些内容
@pytest.mark.skipif(sys.version_info < (3, 6), reason='version is low')
def test_version():
    print("你使用的版本正合适")
    assert 1


@pytest.mark.skipif(sys.version_info < (3, 9), reason='version is low')
def test_version_now():
    print("你使用的版本不合适")
    assert 1


# 创建共享标记
mark_skip_if = pytest.mark.skipif(sys.version_info < (3, 9), reason='version is disabled')


@mark_skip_if
def test_version_new():
    print("你使用的版本不合适")
    assert 1


'''
    如果将多个skipif装饰器应用于一个测试功能,则在任何跳过条件为true的情况下将被跳过
'''

# pytest.importorskip 跳过缺少的导入的测试
# docutils = pytest.importorskip("docutils", minversion="0.3")
#
#
# @docutils
# def test_can_join():
#     print("导入包成功")
#     assert 1


# xfail 标记来期望测试失败
# pytest.mark.xfail(condition = None,*,reason = None,引发= None,run = True,strict = False )
"""
:param strict
如果False(默认值)
    该功能将在终端输出中显示
        就像xfailed它失败了一样,xpass好像它通过了一样。

如果为True,
    则该函数将在终端输出中显示为xfailed失败,
        但是如果意外通过,则它将使测试套件失败
"""


@pytest.mark.xfail
def test_function_xfail():
    print("我期望他失败")
    assert 1


# 自定义标记(需要注册)
@pytest.mark.timeout(10, 'slow', method='thread')
def test_function_timeout():
    print("运行时间过长")
    assert 1


# 传递参数
@pytest.mark.parametrize('one', [1, 2])
def test_func1(one):
    print(one + 1, end=' ')
    assert 1


# 传递多个参数
@pytest.mark.parametrize('one,two', [(1, 4), (2, 8)])
def test_func1(one,two):
    print(one + two, end=' ')
    assert 1


# skip与参数多态
@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

猜你喜欢

转载自blog.csdn.net/hide_in_darkness/article/details/108439181