Pytest是一個基于python的測試框架,用于編寫和執(zhí)行測試代碼。pytest主要用于API測試,可以編寫代碼來測試API、數據庫、UI等。
pytest是一個非常成熟的全功能的Python測試框架,主要有以下幾個優(yōu)點:
簡單靈活,容易上手。pytest的語法簡潔明了,易于理解和使用。
支持參數化。pytest可以通過裝飾器或 fixture 方法對測試用例進行參數化,提高測試用例的覆蓋率。
能夠支持簡單的單元測試和復雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests)。
pytest具有很多第三方插件,并且可以自定義擴展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報告生成)、pytest-rerunfailures(失敗case重復執(zhí)行)、pytest-xdist(多CPU分發(fā))等。
測試用例的skip和xfail處理。pytest提供了靈活的跳過測試用例或預期失敗的機制,可以根據需要在測試過程中跳過某個或某些測試用例。
可以很好的和jenkins集成。pytest可以和Jenkins等持續(xù)集成工具無縫集成,方便進行自動化測試和報告生成。
report框架----allure 也支持了pytest。pytest可以和Allure報告框架集成,生成詳細的HTML測試報告,方便進行測試結果分析和展示。
pytest是一個功能強大、靈活易用的Python測試框架,適用于各種類型的測試需求,具有很高的實用價值。
安裝
# 安裝
pip install pytest
# 幫助
pytest -h
格式要求
文件名稱:test_*.py 或 *_test.py
函數名:test開頭
常用參數
-s
顯示標準輸出,相當于–capture=no
,pytest默認是不輸出print logging
等的輸出的,除非assert失敗。-v
顯示詳細報告。-k
按照關鍵詞查找測試用例。-q
顯示簡潔報告。-m
只運行被標記的測試用例。-x
用例失敗時立即停止測試。-c file
從 file 加載配置文件。-l (--showlocals)
用例失敗信息回溯時顯示局部變量及其值。-rsxX
報告?測試用例被跳過(s)、預計失敗(x)、預計失敗但實際通過(X)的原因。-strict
禁止使用未在配置文件(pytest.ini)注冊的 mark 標記。--maxfail=n
失敗n后停止運行測試。–reruns=num
失敗用例重跑num次。需要安裝 pytest-rerunfailures
插件模塊。--lf (--last-failed)
僅執(zhí)行上次失敗的用例。如果沒有失敗的用例或者沒找到緩存文件,默認是運行所有的用例。 --lfnf =[all, none]
與 --lf
同時使用,=all
代表找不到用例或緩存文件時執(zhí)行所有用例,=none
代表找不到用 例或緩存文件時不執(zhí)行測試用例。
pytest.main(['--lf','--lfnf=none', "test.py"])
--ff (--failed-first)
先執(zhí)行失敗的用例,再執(zhí)行其他用例。--nf (--new-first)
首先從新文件或新修改的用例開始運行測試。--sw (--stepwise)
在測試失敗時退出,且下一次在測試失敗的用例開始測試。--stepwise-skip
忽略第一個失敗的測試,在第二次測試失敗時退出。--keep-duplicates
不斷重復的測試。--durations=n
顯示執(zhí)行最慢的n條用例。注意:除非添加參數 -vv,默認情況下,否則pytest不會顯示<0.01s的測試時間。--fixtures
顯示所有可用的 fixture。--tb=style
堆?;厮菪畔⒋蛴∧J?(auto/long/short/line/native/no])。--setup-show
顯示fixture執(zhí)行步驟。--cache-show=[CACHESHOW]
顯示緩存內容,不執(zhí)行收集或測試。--cache-clear
運行前清除pytest緩存。--continue-on-collection-errors
即使發(fā)生收集(收集用例階段)錯誤,也強制執(zhí)行測試。--rootdir=ROOTDIR
定義測試的根目錄。--color=color
終端輸出的顏色(yes/no/auto)。--collect-only
只收集用例,不執(zhí)行。--assert=MODE
“plain”不執(zhí)行任何斷言調試,“rewrite”重寫測試模塊中的assert語句,以提供assert表達式信息
基礎測試
文件名:test_one.py
# 測試函數
def test_division():
assert 1/1.0==1
# 測試類
class TestOne:
def test_addition(self):
"""
測試方法
"""
assert 1 + 1 == 2
def testsquare(self):
"""
測試方法
"""
assert 2*2 == 3
def tesequality():
"""
無效
"""
assert 10 == 11
運行:
pytest -v
-v
表示查看詳情。
找到3個測試用例,1個失敗,2個通過。
測試子集
按照函數名查找子集
test_sub.py
# 測試子集
class TestSub:
def test_compare_one(self):
"""
測試方法
"""
assert 1 + 1 == 2
def test_compare_two(self):
"""
測試方法
"""
assert 1 + 2 == 3
pytest -v -k compare
使用pytest -k <substring>
命令的-k
參數值來過濾函數名。
分組標記
# -*- coding: utf-8 -*-
import pytest
# 測試子集
class TestGroup:
@pytest.mark.group
def test_group_one(self):
"""
測試方法
"""
assert 1 + 1 == 2
@pytest.mark.group
def test_group_two(self):
"""
測試方法
"""
assert 1 + 2 == 3
pytest -v -m group
這里用裝飾器 @pytest.mark.group
來標記函數,然后用pytest -v -m group
中的-m
來尋找這個分組標記。
夾具函數
import pytest
# 測試fixture
class TestFixture:
@pytest.fixture
def input_value(self):
return 36
def test_division(self, input_value):
"""
測試方法
"""
assert input_value / 6 == 6
這里用@pytest.fixture
修飾的函數input_value提前準備了數據,以供test_division
用。這種方法只能在一個文件里用,如果想全局使用可以配置Conftest.py。
參數化
import pytest
@pytest.mark.parametrize("num, output",[(1,11),(2,22),(3,35),(4,44)])
def test_multiplication_11(num, output):
assert 11*num == output
pytest test_parame.py -v
跳過測試
import pytest
@pytest.mark.xfail
@pytest.mark.great
def test_greater():
num = 100
assert num > 100
@pytest.mark.xfail
@pytest.mark.great
def test_greater_equal():
num = 100
assert num >= 100
@pytest.mark.skip
@pytest.mark.others
def test_less():
num = 100
assert num < 200
pytest test_xfail_skip.py -v
@pytest.mark.xfail
標記為xfail狀態(tài)。@pytest.mark.skip
直接跳過。
文章來源:http://www.zghlxwxcb.cn/news/detail-641093.html
更多請見官網。文章來源地址http://www.zghlxwxcb.cn/news/detail-641093.html
到了這里,關于Python測試框架pytest:常用參數、查找子集、參數化、跳過的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!