用例打标记

一、一些内置的标记
@pytest.mark.skip,在用例方法上使用可以跳过该用例的执行
@pytest.mark.xfail,预期不会通过的用例,执行后该用例的结果状态就是xfail,不会按用例未通过处理

执行的时候,直接执行,不必像自定义标记中pytest -m 标记名
import pytest


@pytest.mark.skip
def test_demo2_02():
    assert 99 == 99

@pytest.mark.xfail(reason='已知的不通过用例')
def test_demo2_01():
    assert 99 == 999

二、自定义的标记
step1、注册标记:
①在启动文件所在目录下创建一个 pytest.ini的配置文件,必须是这个名字(该配置文件和启动文件是同级别文件)
②在pytest.ini中添加一个 pytest的配置块
③在pytest 的配置块下面加一个markers的配置项
④在markers的配置项中去注册标记
[pytest]
markers =
    yuan
    chun
    class_

step2、给对应的用例打标记:@pytest.mark.注册的标记名称
注:可以给一条用例打多个标记
import pytest

@pytest.mark.yuan
def test_demo2_03(): assert 9 == 9 @pytest.mark.chun @pytest.mark.yuan def test_demo2_05(): assert 999 == 999 def test_demo2_06(): assert 999 == 999

step3、执行
①pytest -m 标记名称
②执行多个标记的用例:pytest -m "标记名1 or 标记名2"
如 pytest -m "yuan or chun",那么所有yuan和chun标记的用例都会执行
注:一定要用双引号
③打了某个标记的用例不执行:pytest -m "not 标记名"
④某条用例被打了两个标记,执行该用例:pytest -m "标记名1 and 标记名2"

另:在pycharm运行文件中的执行方式:pytest.main(["-m 标记名"])

三、类标记
"""
# 方式一:直接类上面打标记,如:
@pytest.mark.webtest
class TestClass(object):

# 方式二:通过类属性pytestmark,如:
class TestClass(object):
    pytestmark = [pytest.mark.webtest, pytest.mark.slowtest]
"""

import pytest


@pytest.mark.yuan
@pytest.mark.class_
class TestDemo1:
    def test_demo1_01(self):
        assert 1 == 100

    def test_demo1_02(self):
        assert 100 == 100


class TestDemo2:
    pytestmark = [pytest.mark.class_, pytest.mark.yuan]

    def test_demo1_01(self):
        assert 1 == 100

    def test_demo1_02(self):
        assert 100 == 100
posted @ 2022-01-08 16:32  2orange  阅读(58)  评论(0编辑  收藏  举报