天天看點

Pytest 使用簡介

前言

  最近在聽極客時間的課程,裡面的講師極力推崇 pytest 架構,鄙視 unittest 架構,哈哈!然後查了些資料,發現了一條 python 鄙視鍊:pytest 鄙視 > unittest 鄙視 >  robotframework 。

  pytest 是 python 的第三方單元測試架構,比自帶 unittest 更簡潔和高效,支援315種以上的插件,同時相容 unittest 架構。這就使得我們在 unittest 架構遷移到 pytest 架構的時候不需要重寫代碼。接下來我們在文中來對分析下 pytest 有哪些簡潔、高效的用法。

一、安裝

首先使用 pip 安裝 pytest 

pip3 install pytest

 檢視 pytest 是否安裝成功

pip3 show pytest

二、簡單使用

1.建立 test_sample.py 檔案,代碼如下:

#!/usr/bin/env python
# coding=utf-8
import pytest

def inc(x):
    return x + 1

def test_answer():
    assert inc(3) == 5

if __name__ =="__main__":
    pytest.main()      

執行結果:

test_sample.py F                                                         [100%]

================================== FAILURES ===================================
_________________________________ test_answer _________________________________

    def test_answer():
>       assert inc(3) == 5
E       assert 4 == 5
E        +  where 4 = inc(3)

test_sample.py:19: AssertionError
============================== 1 failed in 0.41s ==============================      

從上面的例子可以看出,pytest 中斷言的用法直接使用 assert ,和 unittest 中斷言 self.assert 用法有所差別。

2.總結一下:使用 pytest 執行測試需要遵行的規則:

  • .py 測試檔案必須以test_開頭(或者以_test結尾) 
  • 測試類必須以Test開頭,并且不能有 init 方法
  • 測試方法必須以test_開頭
  • 斷言必須使用 assert

三、fixture

 pytest 提供的 fixture 實作 unittest  中 setup/teardown 功能,可以在每次執行case之前初始化資料。不同點是,fixture 可以隻在執行某幾個特定 case 前運作,隻需要在運作 case 前調用即可。比 setup/teardown 使用起來更靈活。

1.fixture scope 作用範圍

 先看下 fixture 函數的定義:

def fixture(scope="function", params=None, autouse=False, ids=None, name=None):
    """
    :arg scope:    可選四組參數:function(預設)、calss、module、package/session

    :arg params:   一個可選的參數清單,它将導緻多個參數調用fixture函數和所有測試使用它。

    :arg autouse:  如果為True,則fixture func将為所有測試激活可以看到它。如果為False(預設值),則需要顯式激活fixture。

    :arg ids:      每個參數對應的字元串id清單,是以它們是測試id的一部分。如果沒有提供id,它們将從參數中自動生成。

    :arg name:     fixture的名稱。 這預設為裝飾函數的名稱。 如果fixture在定義它的同一子產品中使用,夾具的功能名稱将被請求夾具的功能arg遮蔽; 解決這個問題的一種方法是将裝飾函數命名 “fixture_ <fixturename>”然後使用”@ pytest.fixture(name ='<fixturename>')”。
  """      

 重點說下 scope 四組參數的意義:

  • function:每個方法(函數)都會執行一次。
  • class:每個類都會執行一次。類中有多個方法調用,隻在第一個方法調用時執行。
  • module:一個 .py 檔案執行一次。一個.py 檔案可能包含多個類和方法。
  • package/session:多個檔案調用一次,可以跨 .py 檔案。

 在所需要調用的函數前面加個裝飾器 @pytest.fixture()。舉一個簡單的例子:

#!/usr/bin/env python
# coding=utf-8
import pytest

@pytest.fixture(scope='function')
def login():
    print("登入")

def test_1():
    print('測試用例1')

def test_2(login):
    print('測試用例2')


if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])      

 執行結果:

test_sample.py 
測試用例1
.
登入
測試用例2
.

============================== 2 passed in 0.07s ==============================      

 2.yield

 我們剛剛實作了在每個用例之前執行初始化操作,那麼用例執行完之後如需要 清除資料(或還原)操作,可以使用 yield 來實作。

#!/usr/bin/env python
# coding=utf-8
import pytest

@pytest.fixture(scope='function')
def login():
    print("登入")
    yield
    print("登出登入")

def test_1():
    print('測試用例1')

def test_2(login):
    print('測試用例2')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])      
test_sample.py 
測試用例1
.
登入
測試用例2
.登出登入

============================== 2 passed in 0.08s ==============================      

 3.conftest

 上面的案例都是寫在同一個.py 檔案内的。倘若有多個.py 檔案需要調用 login() 方法,就必須把 login() 方法寫在外面,這裡引用了conftest.py 配置檔案。test_xxx.py 測試檔案中無需 import  conftest,pytest 會自動搜尋同級目錄中的 conftest.py 檔案。

 conftest.py 與 測試檔案 目錄層級關系

Pytest 使用簡介
# 建立conftest.py,和 test_sample.py 同級目錄
import pytest

@pytest.fixture(scope='function')
def login():
    print("登入")




# test_sample.py 代碼如下
import pytest

def test_1():
    print('測試用例1')

def test_2(login):
    print('測試用例2')

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])      
test_sample.py 
測試用例1
.
登入
測試用例2
.

============================== 2 passed in 0.01s ==============================      

四、重試機制

有的時候用例執行失敗了,然後排查發現不是代碼問題,可能和環境或者網絡不穩定有關系,這個時候可以引入重試機制,排除一些外在因素。

1、安裝 pytest-rerunfailures

pip3 install pytest-rerunfailures

2、重試的兩種方法

1)使用裝飾器 @pytest.mark.flaky(reruns=5, reruns_delay=2) 

  • reruns :最大重試次數
  • reruns_delay :重試間隔時間,機關是秒
#!/usr/bin/env python
# coding=utf-8
import pytest


@pytest.mark.flaky(reruns=5, reruns_delay=2)
def test():
    assert 0==1

if __name__ =="__main__":
    pytest.main(['test_sample.py','-s'])      
Pytest 使用簡介

 R表示用例失敗後正在重試,嘗試5次。

2)也可以使用指令行 pytest --reruns 5 --reruns-delay 2 -s ,參數與裝飾器 @pytest.mark.flaky 一緻,這個就不多說了。

繼續閱讀