@pytest.mark 其他用法

# coding=utf-8
# pytest -q test_example.py

import pytest


class TestCasse( ):
    """
    pytest test_example.py -m case10
    pytest test_example.py -m "case12 or case13"
    pytest test_example.py -m "not case13"

    """

    @pytest.mark.case10
    def test_case10(self):
        assert 4 == 3, "两个数不相等"

    def test_case11(self):
        assert 3 == 3, "两个数相等"

    @pytest.mark.case12
    def test_case12(self):
        assert 4 == 3, "两个数不相等"

    @pytest.mark.case13
    def test_case13(self):
        assert 5 == 3, "两个数不相等"

    @pytest.mark.skip  # skip可以放在类前(跳过类),也可以放在方法前(跳过方法)
    def test_case14(self):
        assert 11 == 3, "两个数不相等"


def f( ):
    raise SystemExit(1)


def test_mytest( ):
    with pytest.raises(SystemExit):
        f( )


def hello( ):
    return "hello world"


# 1.跳过这个测试
# @pytest.mark.skip(reason = "跳过这个测试")
# 2.有条件的跳过这个测试
# @pytest.mark.skipif(sys.version_info >= (3,6), reason = "有条件的跳过这个测试")
# 3.strict=True以确保XPASS(意外通过)时,测试的记录为失败
# @pytest.mark.xfail(strict = True)
# 4.标记对故障的期待,如果测试失败且未在加注中提及,则将报告为常规故障
# @pytest.mark.xfail(sys.version_info >= (3, 6), reason = "python3.6 api changes")
# 5.raises参数中指定单个异常或异常组,如果错误类型是别的,就会打印错误
# @pytest.mark.xfail(raises = RuntimeError)
def test_hello( ):
    assert hello( ) == "hello world"


@pytest.mark.run_these_test  # 打上了run_these_test标签
def test_zero_division( ):
    with pytest.raises(ZeroDivisionError) as exinfo:
        print(1 / 0)
    assert exinfo.type == ZeroDivisionError
    # 除以零    整数除或以零为模
    assert str(exinfo.value) == "division by zero"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值