Pytest 單元測試框架標記用例
阿新 • • 發佈:2020-07-02
1、Pytest 中標記用例
- 接引數 -k 來挑選要執行的測試項
- pytest -k test_szdcs -s
- test_szdcs 為函式名稱
- -k 後面接的名稱可以為函式名稱、類名稱、檔名稱、目錄名稱
- 區分大小寫
- 支援模糊匹配
- 可以用not表示選擇用例名稱中不包含哪些內容,如下
- pytest -k test_szdcs -s
class Test01():
def test_szdcs(self):
print("深圳多測師")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -k "not sz" -s# 結果如下
test_demo1.py
廣州多測師.
上海多測師.
- 可以用and表示選擇的用例名稱中同時包含多個關鍵字,如下
class Test01():
def test_szdcs(self):
print("深圳多測師")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -k "g and z" -s
# 結果如下
test_demo1.py廣州多測師.
- 可以用or表示選擇的用例名稱中包含其中一個關鍵字即可,如下
class Test01():
def test_szdcs(self):
print("深圳多測師")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -k "sh or sz" -s
# 結果如下
test_demo1.py
深圳多測師.
上海多測師.
- 指定標籤執行需要執行的測試項
- @pytest .mark.tag
- tag 為自定義的標籤名稱
- 可以對方法加上標籤
- 也可以對整個類加上標籤
- 也可以定義全域性標籤
- pytest test_demo1.py -m tag -s
- pytest 後面可以接類名稱、檔名稱、目錄名稱
- -m 後面接標籤名稱
- @pytest .mark.tag
# 對方法加標籤
import pytest class Test01():
def test_szdcs(self):
print("深圳多測師") @pytest.mark.tag
# 通過裝飾器定義標籤,自定義標籤名稱 tag
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest test_demo1.py -m tag -s
# 結果如下
test_demo1.py
廣州多測師.
# 對類加標籤
import pytest class Test01():
def test_szdcs(self):
print("深圳多測師") def test_gzdcs(self):
print("廣州多測師") @pytest.mark.tag
class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest test_demo1.py -m tag -s
# 結果如下
test_demo1.py
上海多測師.
import pytest
# 定義全域性標籤
mark = pytest.mark.tag class Test01():
def test_szdcs(self):
print("深圳多測師")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -m tag -s
# 結果如下
test_demo1.py
深圳多測師.
廣州多測師.
上海多測師.
2、Pytest 中 skip 跳過用例
- 標記 skip 表示跳過該用例,不執行
- pytest.mark.skip(reason="str")
# 在方法上條件裝飾器 skip,跳過單條用例
import pytest class Test01():
def test_szdcs(self):
print("深圳多測師") @pytest.mark.skip(reason="跳過的用例")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -sv test_demo1.py
# 結果如下
test_demo1.py::Test01::test_szdcs 深圳多測師
PASSED
test_demo1.py::Test01::test_gzdcs
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多測師
PASSED
# 在類上面新增裝飾器 skip,跳過整個類中的所有用例
import pytest @pytest.mark.skip(reason="跳過的用例")
class Test01():
def test_szdcs(self):
print("深圳多測師") def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -sv test_demo1.py
# 結果如下
test_demo1.py::Test01::test_szdcs
SKIPPED
test_demo1.py::Test01::test_gzdcs
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多測師
PASSED
3、Pytest 中 skipif 跳過用例
- 通過條件判斷是否忽略不執行該用例
- 判斷條件表示式 pytest.mark.skipif(condition,reason="str")
import pytest class Test01():
def test_szdcs(self):
print("深圳多測師") @pytest.mark.skipif(2>1,reason="條件正確不執行")
def test_gzdcs(self):
print("廣州多測師") class Test02():
def test_shdcs(self):
print("上海多測師") # 執行命令 pytest -sv test_demo1.py
# 結果如下
test_demo1.py::Test01::test_szdcs 深圳多測師
PASSED
test_demo1.py::Test01::test_gzdcs
SKIPPED
test_demo1.py::Test02::test_shdcs 上海多測師
PASSED