zoukankan      html  css  js  c++  java
  • pytest 给用例打标签

    1.给用例添加自定义标签命令:@pytest.mark.tagname     #tagname是自定义的标签名

    import pytest
    
    class TestClass():
        @pytest.mark.smoke
        def test_one(self):
            print("test_one方法执行")
            assert  1==1
        @pytest.mark.回归测试
        def test_two(self):
            print("test_two方法执行")
            assert  'o' in 'love'

    2.根据标签运行测试用例:pytest -m tagname

    C:Userscalecheckapi	est_cc>pytest test_m1.py -m smoke -s
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
    rootdir: C:Userscalecheckapi	est_cc
    plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items / 1 deselected / 1 selected
    
    test_m1.py test_one方法执行
    .
    
    ============================================================================================================= warnings summary =============================================================================================================
    c:usersipharmacarepython37libsite-packages\_pytestmarkstructures.py:325
      c:usersipharmacarepython37libsite-packages\_pytestmarkstructures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.smoke - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
        PytestUnknownMarkWarning,
    
    c:usersipharmacarepython37libsite-packages\_pytestmarkstructures.py:325
      c:usersipharmacarepython37libsite-packages\_pytestmarkstructures.py:325: PytestUnknownMarkWarning: Unknown pytest.mark.回归测试 - is this a typo?  You can register custom marks to avoid this warning - for details, see https://docs.pytest.org/en/latest/mark.html
        PytestUnknownMarkWarning,
    
    -- Docs: https://docs.pytest.org/en/latest/warnings.html
    =============================================================================================== 1 passed, 1 deselected, 2 warnings in 0.22s ================================================================================================

    3.因为自定义的标签没有注册,所以在运行时会出现警告的信息,pytest注册标签有两种方法

    (1):注册pytest.ini文件(在当前目录创建pytest.ini文件)

    [pytest]
    markers=
         smoke:冒烟测试   #冒号后面是标签的描述,可不填
         two
         回归测试         #如果标签是中文,编码需对应进行修改

    再看下运行结果:警示信息没有了

    C:Userscalecheckapi	est_cc>pytest test_m1.py  -s
    ========================================================================= test session starts =========================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
    rootdir: C:Userscalecheckapi	est_cc, inifile: pytest.ini
    plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items
    
    test_m1.py test_one方法执行
    .test_two方法执行
    .
    
    ========================================================================== 2 passed in 0.13s ==========================================================================

    (2):写钩子到conftest.py(在命令行当前目录下创建conftest.py)

    def pytest_configure(config):
        config.addinivalue_line("markers",'smoke')
        config.addinivalue_line("markers", '回归测试')

    运行结果如下 

    C:Userscalecheckapi	est_cc>pytest test_m1.py  -s
    =========================================================================================================== test session starts ============================================================================================================
    platform win32 -- Python 3.7.3, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
    rootdir: C:Userscalecheckapi	est_cc
    plugins: allure-pytest-2.8.13, html-2.0.0, metadata-1.8.0, rerunfailures-7.0
    collected 2 items
    
    test_m1.py test_one方法执行
    .test_two方法执行
    .
    
    ============================================================================================================ 2 passed in 0.09s =============================================================================================================

     4.在一个测试用例上打多个标签

    import pytest
    
    
    @pytest.mark.smoke
    @pytest.mark.回归测试
    def test_one(self):
            print("test_one方法执行")
            assert  1==1

    5.给测试类打标签

    import pytest
    
    #方式一 @pytest.mark.smoke
    class TestClass(): def test_one(self): print("test_one方法执行") assert 1==1
    #方式二
    class TestClass():
        pytestmark=[pytest.mark.smoke,pytest.mark.回归测试] #多个标签存在列表里面
          def test_one(self):
                print("test_one方法执行")
                assert  1==1
  • 相关阅读:
    Linux 常用命令总结(二)
    Linux(CentOS7)使用 RPM 安装 mysql 8.0.11
    Linux(CentOS7) 相关软件安装
    8、js——字符串
    7、js——数组
    6、js——创建对象方式
    5、js——this说明
    4、js——函数
    4、js——对象
    2、js编写位置
  • 原文地址:https://www.cnblogs.com/pipile/p/12696226.html
Copyright © 2011-2022 走看看