# 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"
@pytest.mark 其他用法
最新推荐文章于 2024-08-31 17:27:37 发布