Pytest测试框架(5):mark用法---测试用例分组执行

pytest中的mark:

  mark主要用于在测试用例/测试类中给用例打标记(只能使用已注册的标记名),实现测试分组功能,并能和其它插件配合设置测试方法执行顺序等。

 

如下图,现在需要只执行红色部分的测试方法,其它方法不执行:

 

 

 

设置步骤如下:

1、注册标签名,通过pytest.ini配置文件注册;
2、在测试用例的前面加上:@pytest.mark.已注册标签名
3、运行时,根据用例标签过滤(-m标签名)


 

通过mark分组执行的用例:

1、在项目下新建一个pytest.ini的配置文件;如下图:

 

 

 2、在用例里面添加@pytest.mark.标签名;如下代码:

代码示例:

test_demo_mark_01.py文件

import pytest

class TestDemo01:

    def test_case_01(self):
        print('exec TestDemo01 test_case_01',end=' ')
        assert True
    @pytest.mark.system_test
    def test_case_02(self):
        print('exec TestDemo01 test_case_02',end=' ')
        assert True

test_demo_mark_02.py文件

import pytest

class TestDemo02:
    @pytest.mark.login_module
    @pytest.mark.system_test
    def test_case_01(self):
        print('exec TestDemo02 test_case_01',end=' ')
        assert True

    def test_case_02(self):
        print('exec TestDemo02 test_case_02',end=' ')
        assert True

test_demo_mark_03.py文件

import pytest

class TestDemo03:
    @pytest.mark.smoke_test
    @pytest.mark.login_module
    def test_case_01(self):
        print('exec TestDemo03 test_case_01',end=' ')
        assert True

    def test_case_02(self):
        print('exec TestDemo03 test_case_02',end=' ')
        assert True

3、在run_case.py文件中执行pytest.main(['-s','-v','-m 标签名')

代码示例:

import pytest

pytest.main(['-s','-v','-m smoke_test'])                    # 只执行smoke_test分组下的用例
# pytest.main(['-s','-v','-m system_test or login_module'])   # 两个标签的用例都执行
# pytest.main(['-s','-v','-m system_test and login_module'])  # 包含两个标签的用例才执行
# pytest.main(['-s','-v','-m not login_module'])                # 除了该标签之外,其他用例都执行

执行效果:

 

posted @ 2021-10-18 22:29  奔跑在路上you  阅读(621)  评论(0编辑  收藏  举报