目錄
- 1、前言
- 2、mark的使用
- (一)注冊自定義標記
- (二)在測驗用例上標記
- (三)執行
- 3、擴展
- (一)在同一個測驗用例上使用多個標記
- (二)在測驗類上使用標記
1、前言
在自動化測驗作業中我們有時候并不需要測驗所有的測驗用例,比如在冒煙測驗階段,我們只需要測驗基本功能是否正常就可以了,在pytest中提供了mark標記功能來實作分組執行,
2、mark的使用
步驟:
- 在
pytest.ini中注冊標記(名稱可自定義) - 使用
@pytest.mark.上一步注冊的名稱標記需要執行的用例 - 執行
(一)注冊自定義標記
在pytest.ini中添加markers
[pytest] # 固定的section名
markers= # 固定的option名稱,注意縮進,
標簽名1: 標簽名的說明內容,
標簽名2: 不寫也可以
標簽名N
示例:
[pytest]
markers =
smoke: 冒煙測驗
back
(二)在測驗用例上標記
通過@pytest.mark.標記名標記要測驗的用例,
示例:
import pytest
@pytest.mark.smoke
def test_1():
print("測驗1")
@pytest.mark.back
def test_2():
print("測驗2")
def test_3():
print("測驗3")
(三)執行
通過在命令列增加-m引數指定要測驗的分組,
示例:
執行冒煙用例:pytest -vs xxx.py -m smoke
"""
執行結果
collected 3 items / 2 deselected / 1 selected
mark/mark/mark.py::test_1 測驗1
PASSED
"""
執行回歸用例:pytest -vs xxx.py -m back
"""
執行結果
collected 3 items / 2 deselected / 1 selected
mark/mark/mark.py::test_2 測驗2
PASSED
"""
3、擴展
(一)在同一個測驗用例上使用多個標記
import pytest
@pytest.mark.back
def test_register():
# 注冊
print("注冊")
@pytest.mark.smoke
@pytest.mark.back
def test_login():
# 登錄
print("登錄")
@pytest.mark.back
def test_logout():
# 注銷
print("注銷")
@pytest.mark.smoke
def test_add_cart():
# 加購物車
print("加購物車")
@pytest.mark.smoke
def test_place_order():
# 下單
print("下單")
@pytest.mark.smoke
@pytest.mark.pay
def test_pay_order():
# 支付訂單
print("支付訂單")
在執行時支持通過and,or,or來連接多個標記,如下
pytest -vs -m "smoke or pay" xxx.py,此時只有登錄,加購物車,下單,支付訂單這4個用例執行pytest -vs -m "smoke and pay" xxx.py,此時只有支付訂單這個用例執行pytest -vs -m "not smoke" xxx.py,此時只有注冊,注銷這2個用例執行
(二)在測驗類上使用標記
import pytest
@pytest.mark.smoke
class TestLogin:
def test_login(self):
print("登錄")
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/502379.html
標籤:Python
