• pytest框架之mark标签


    对测试用例打标签,在运行测试用例的时候,可根据标签名来过滤要运行的用例。

    一、注册标签名

    1.创建pytest.ini文件,在文件中按如下方式添加标签名:

    [pytest]
    markers =
        smoke:marks tests as smoke.
        demo:marks tests asa demo.

     备注:冒号之后是描述信息(可写可不写)。

    2.在conftest.py文件当中,通过hock注册:

    def pytest_configure(config):
        config.addinivalue_line("markers", "smoke:标记只运行冒烟用例")
        config.addinivalue_line("markers", "demo:标记只运行示例用例")

    二、打标签

    打标记的范围:测试用例、测试类、模块文件

    1.方法一

    在测试用例/测试类前加上:@pytest.mark.标记名

    import pytest
    
    
    @pytest.mark.smoke
    def test_add_01():
        b = 1 + 2
        assert 3 == b
    
    
    @pytest.mark.demo
    def test_add_02():
        b = 1 + 2
        assert 0 == b
    
    
    @pytest.mark.smoke
    class TestAdd:
    
        def test_add_03(self):
            b = 1 + 2
            assert 3 == b
    
        def test_add_04(self):
            b = 1 + 1
            assert 2 == b

    也可以在一个用例上打多个标签,多次使用@pytest.mark.标签名

    @pytest.mark.demo
    @pytest.mark.smoke
    def test_add_02():
        b = 1 + 2
        assert 0 == b

    2.方法二

    • 在测试类里面,使用以下声明(测试类下,所有用例都被打上了该标签):
    @pytest.mark.demo
    def test_add_02():
        b = 1 + 2
        assert 0 == b
    
    
    class TestAdd:
    
        pytestmark = pytest.mark.smoke
    
        def test_add_03(self):
            b = 1 + 2
            print(f'b={b}')
            assert 3 == b
    
        def test_add_04(self):
            b = 1 + 1
            print(f'b={b}')
            assert 2 == b

    多标签模式:pytestmark = [pytest.mark.标签1, pytest.mark.标签2......] 

    class TestAdd:
    
        pytestmark = [pytest.mark.smoke, pytest.mark.demo]
    
        def test_add_03(self):
            b = 1 + 2
            print(f'b={b}')
            assert 3 == b
    
        def test_add_04(self):
            b = 1 + 1
            print(f'b={b}')
            assert 2 == b
    • 在模块文件里,同理(py文件下,所有测试函数和测试类里的测试函数,都有该标签):
    import pytest
    pytestmark = pytest.mark.smoke
    pytestmark = [pytest.mark.smoke, pytest.mark.demo]  # 多标签模式

    三、命令行

    根据测试用例/测试类/测试模块,标记了对应的标签后,使用对应的命令行在cmd中或者Pycharm中的Terminal中运行,即可进行用例的筛选,命令行为:

    pytest -m 标签名

    四、pytest收集测试用例的规则

    1.默认从当前目录中收集测试用例,即在哪个目录下运行pytest命令,则从哪个目录当中搜索

    2.搜索规则:

      符合命名规则test_*.py 或者 *_test.py的文件

      以test开头的函数名

      以Test开头的测试类(没有__init__方法)当中,以test_开头的方法

  • 相关阅读:
    centos 安装 Lamp(Linux + Apache + PHP) 并安装 phpmyadmin
    mysql常用内置函数-查询语句中不能使用strtotime()函数!
    Windows下 wamp下Apache配置虚拟域名
    thinkphp ajax调用demo
    phpMailer 手册
    wampServer2.2 You don't have permission to access /phpmyadmin/ on this server.
    打印对象
    最全的CSS浏览器兼容问题
    html 视频播放器
    C语言入门-结构类型
  • 原文地址:https://www.cnblogs.com/xiaogongjin/p/11683530.html
Copyright © 2020-2023  润新知