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:\Users\cale\checkapi\test_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:\Users\cale\checkapi\test_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:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
  c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.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:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.py:325
  c:\users\ipharmacare\python37\lib\site-packages\_pytest\mark\structures.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:\Users\cale\checkapi\test_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:\Users\cale\checkapi\test_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:\Users\cale\checkapi\test_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:\Users\cale\checkapi\test_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 =============================================================================================================

猜你喜欢

转载自www.cnblogs.com/pipile/p/12696226.html
今日推荐