Python之pytest 基礎


pytest是一個非常成熟的全功能的Python測試框架,主要特點有以下幾點:1、簡單靈活,容易上手;2、支持參數化;3、能夠支持簡單的單元測試和復雜的功能測試,還可以用來做selenium/appnium等自動化測試、接口自動化測試(pytest+requests);4、pytest具有很多第三方插件,並且可以自定義擴展,比較好用的如pytest-selenium(集成selenium)、pytest-html(完美html測試報告生成)、pytest-rerunfailures(失敗case重復執行)、pytest-xdist(多CPU分發)等;5、測試用例的skip和xfail處理;6、可以很好的和jenkins集成;(二)安裝

  pip install -U pytest 

  pip install -U pytest-html

  pip install -U pytest-rerunfailures

此外還有很多很好的第三方插件,請到http://plugincompat.herokuapp.com/ 和 https://pypi.python.org/pypi?%3Aaction=search&term=pytest-&submit=search 查找

(三)例子

這里列幾個pytest-document中的例子

1、默認執行當前目錄下的所有以test_為前綴(test_*.py)或以_test為后綴(*_test.py)的文件中以test_為前綴的函數

復制代碼
import pytest # content of test_sample.py def func(x): return x + 1 def test_answer(): assert func(3) == 5
復制代碼

運行 py.test  或 指定特定文件 py.test -q test_sample.py

2、使用類來組成多個用例的

復制代碼
import pytest # content of test_class.py class TestClass: def test_one(self): x = "this" assert 'h' in x def test_two(self): x = "hello" assert hasattr(x, 'check')
復制代碼

3、在python中調用pytest: python test_class.py

復制代碼
import pytest # content of test_class.py class TestClass: def test_one(self): print 'one' x = "this" assert 'h' in x def test_two(self): print 'two' x = "hello" assert hasattr(x, 'check') if __name__ == '__main__': pytest.main("-q --html=a.html")
復制代碼

4、來個支持參數化的例子,參數化使用pytest.mark.parametrize的參數,第一個為變量的元組,第二個是變量賦值的元組列表,具體下面的章節會仔細介紹

# content of test_time.py import pytest from datetime import datetime, timedelta testdata = [ (datetime(2001, 12, 12), datetime(2001, 12, 11), timedelta(1)), (datetime(2001, 12, 11), datetime(2001, 12, 12), timedelta(-1)), ] @pytest.mark.parametrize("a,b,expected", testdata) def test_timedistance_v0(a, b, expected): diff = a - b assert diff == expected @pytest.mark.parametrize("a,b,expected", testdata, ids=["forward", "backward"]) def test_timedistance_v1(a, b, expected): diff = a - b assert diff == expected def idfn(val): if isinstance(val, (datetime,)): # note this wouldn't show any hours/minutes/seconds return val.strftime('%Y%m%d') @pytest.mark.parametrize("a,b,expected", testdata, ids=idfn) def test_timedistance_v2(a, b, expected): diff = a - b assert diff == expected

使用和調用

  • python -m pytest調用:
    • python -m pytest [...] 效果和py.test [...] 一樣
  • 獲取版本,選項名,環境變量
    • py.test --version 看版本
    • py.test --fixtures 查看內置參數
    • py.test -h | --help 命令行和配置文件幫助
  • 失敗后停止
    • 首次失敗后停止執行:py.test -x
    • py.test --maxfail=2 兩次失敗之后停止執行
  • 執行選擇用例
    • py.test test_mod.py,執行模塊中的用例
    • py.test somepath,執行路徑中用例
    • py.test -k stringexpr,執行字符串表達式中的用例,比如"MyClass?and not method",選擇TestMyClass.test_something,排除了TestMyClass.test_method_simple。
    • py.test --pyargs pkg,導入pkg,使用其文件系統位置來查找和執行用例。執行pypkg目錄下的所有用例。
  • 調試輸出:
    • py.test --showlocals 在traceback中顯示本地變量
    • py.test --showlocals 在traceback中顯示本地變量(快捷方式)
    • py.test --tb=long 默認的traceback信息格式化形式
    • py.test --tb=native 標准庫格式化形式
    • py.test --tb=short 更短的格式
    • py.test --tb=line 每個錯誤一行
  • 失敗時調用PDB (Python Debugger):

Python帶有一個內置的Python調試器稱為PDB。pytest可以在命令行選項指定調用:

py.test --pdb

這將每次失敗時調用Python調試器。通常,您可能只希望這樣做的第一個失敗的測試,以 了解某些故障情況: py.test-X - PDB#下降到PDB上的第一次失敗,然后結束測試階段 py.test - PDB - maxfail=3#下降到PDB前三失敗



幾個pytest documentation中的例子:

 
         

例子1:

 
         
import pytest

# content of test_sample.py
def func(x):
    return x + 1
def test_answer():
    assert func(3) == 5
 
         

命令行切換到文件所謂目錄,執行測試(也可以直接在IDE中運行):


image.png

這個測試返回一個失敗報告,因為func(3)不返回5。

 
         

例子2:
當需要編寫多個測試樣例的時候,我們可以將其放到一個測試類當中,如:

 
         
class TestClass:  
    def test_one(self):  
        x = "this"  
        assert 'h' in x  
  
    def test_two(self):  
        x = "hello"  
        assert hasattr(x, 'check') 
 
         

運行以上例子:


image.png

從測試結果中可以看到,該測試共執行了兩個測試樣例,一個失敗一個成功。同樣,我們也看到失敗樣例的詳細信息,和執行過程中的中間結果。-q即-quiet,作用是減少冗長,具體就是不再展示pytest的版本信息。

 
         

如何編寫pytest測試樣例

 
         

通過上面2個實例,我們發現編寫pytest測試樣例非常簡單,只需要按照下面的規則:

 
         
  • 測試文件以test_開頭(以_test結尾也可以)
  • 測試類以Test開頭,並且不能帶有 init 方法
  • 測試函數以test_開頭
  • 斷言使用基本的assert即可
 
         

運行模式

 
         

   Pytest的多種運行模式,讓測試和調試變得更加得心應手,下面介紹5種常用的模式。在介紹之前需要提醒一句,運行pytest時會找當前目錄及其子目錄中的所有test_*.py 或 *_test.py格式的文件以及以test開頭的方法或者class,不然就會提示找不到可以運行的case了。

 
         

1.運行后生成測試報告(htmlReport)

 
         

安裝pytest-html:

 
         
pip install -U pytest-html
 
         

運行模式:

 
         
pytest --html=report.html
 
         

報告效果:


image.png

在以上報告中可以清晰的看到測試結果和錯誤原因,定位問題很容易。

 
         

2.運行指定的case

 
         

  當我們寫了較多的cases時,如果每次都要全部運行一遍,無疑是很浪費時間的,通過指定case來運行就很方便了。

 
         

例子代碼:

 
         
class TestClassOne(object):
    def test_one(self):
        x = "this"
        assert 't'in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')


class TestClassTwo(object):
    def test_one(self):
        x = "iphone"
        assert 'p'in x

    def test_two(self):
        x = "apple"
        assert hasattr(x, 'check')
 
         

運行模式:

 
         

模式1:直接運行test_se.py文件中的所有cases:

 
         
pytest test_se.py
 
         

模式2:運行test_se.py文件中的TestClassOne這個class下的兩個cases:

 
         
pytest test_se.py::TestClassOne
 
         

模式3:運行test_se.py文件中的TestClassTwo這個class下的test_one:

 
         
pytest test_se.py::TestClassTwo::test_one
 
         

注意:定義class時,需要以T開頭,不然pytest是不會去運行該class的。

 
         

3.多進程運行cases

 
         

  當cases量很多時,運行時間也會變的很長,如果想縮短腳本運行的時長,就可以用多進程來運行。

 
         

安裝pytest-xdist:

 
         
pip install -U pytest-xdist
 
         

運行模式:

 
         
pytest test_se.py -n NUM
 
         

其中NUM填寫並發的進程數。

 
         

4.重試運行cases

 
         

  在做接口測試時,有事會遇到503或短時的網絡波動,導致case運行失敗,而這並非是我們期望的結果,此時可以就可以通過重試運行cases的方式來解決。

 
         

安裝pytest-rerunfailures:

 
         
pip install -U pytest-rerunfailures
 
         

運行模式:

 
         
pytest test_se.py --reruns NUM
 
         

NUM填寫重試的次數。

 
         

5.顯示print內容

 
         

  在運行測試腳本時,為了調試或打印一些內容,我們會在代碼中加一些print內容,但是在運行pytest時,這些內容不會顯示出來。如果帶上-s,就可以顯示了。

 
         

運行模式:

 
         
pytest test_se.py -s
 
         

  另外,pytest的多種運行模式是可以疊加執行的,比如說,你想同時運行4個進程,又想打印出print的內容。可以用:

 
         
pytest test_se.py -s -n 4
 
         

學習網站:
pytest documentation
好用的Pytest單元測試框架(《51測試天地》四十九(下)- 44)
Pytest學習筆記
pytest單元測試框架

 

兼容unittest與nose

 
         

如果之前你的單測全部是基於unittest或者nose來編寫的。
不用擔心,執行pytest命令同樣可以正常運行這些用例並得到結果。
因此無需擔心遷移單測框架從而帶來額外的人工成本。

 
         

常用插件

 
         

在之前的內容中,我們提到了pytest的一個優點是有大量的插件支持,例如:

 
         
    • pytest-django: 針對Django框架的單測框架
    • pytest-twisted: 針對twisted框架的單測框架
    • pytest-cov: 產生覆蓋率報告
    • pytest-instafail: 發送錯誤時報告錯誤信息
    • pytest-bdd 測試驅動開發工具
    • pytest-konira 測試驅動開發工具
    • pytest-timeout: 支持超時功能
    • pytest-pep8: 支持PEP8檢查
    • pytest-flakes: 結合pyflakes進行代碼檢查
 
        


免責聲明!

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



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