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 =============================================================================================================

 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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM