Fork me on GitHub

【pytest-03】pytest- 使用自定义标记mark

一、pytest 通过@pytest.mark进行自定义标记,自定义标记可以把一个 web 项目划分多个模块,然后指定模块名称执行

代码参考:

pytest_mark.py:
import pytest


@pytest.mark.test1
def test_weixin():
    print("测试输出1")


@pytest.mark.test2
def test_toutou():
    print("测试输出2")


@pytest.mark.test3
def test_toutuo1():
    print("测试输出3")


@pytest.mark.test4
class TestClass:
    def test_method(self):
        print("测试输出4")


def testnoMark():
    print("没有标记测试")



if __name__ == '__main__':
    pytest.main(['-s', '-m test1', 'pytest_mark.py'])

  

返回结果:

 

 

 二、不想标记test1的用例,我们直接取反即可

if __name__ == '__main__':
    pytest.main(['-s', '-m not test1', 'pytest_mark.py'])

 

 

 三、执行多个自定义标记的用例

if __name__ == '__main__':
    pytest.main(['-s', '-m test1 or test3', 'pytest_mark.py'])

 

 

  

  

posted @ 2022-03-15 23:44  橘子偏爱橙子  阅读(150)  评论(0编辑  收藏  举报