Python pytest測試框架基本用法(一)

2021-10-04 19:43:14 字數 3821 閱讀 8209

一、框架介紹及安裝

pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。

安裝 pip install pytest

我這裡已經安裝成功了

二、框架使用流程

建立如下的原始碼檔案test_one.py

def add(x,y):

return x+y

def test_add_1(

): assert add(1,2)

==3

cmd視窗中切換到原始檔所在的目錄

接著執行pytest檔案,有三種執行方法

執行pytest命令,或py.test命令,或python –m pytest命令。

pytest執行規則

1.查詢當前目錄及其子目錄下以test_*.py或***_test.py檔案。

2.找到檔案後,在檔案中找到以test開頭函式**並執行。

所以你 在建立pytest檔案的時候必須要注意需要以test開頭或者結尾,用例要以test開頭,這個跟unittest是一樣的

如果目錄下有比較多模組你只想執行某乙個的時候可以這樣做

pytest test_one.py(執行第乙個測試檔案)

用例設計原則

檔名以test_.py檔案

test.py

測試類以test開頭,並且不能帶有init方法

以test_開頭的函式

以test開頭的類

以test_開頭的方法

所有的包pakege必須要有__init_.py檔案

斷言使用assert

執行.py模組裡面的某個函式

pytest test_mod.py::test_func

執行.py模組裡面,測試類裡面的某個方法

pytest test_mod.py::testclass::test_method

三、斷言

常用斷言

pytest裡面斷言實際上就是python裡面的assert斷言方法,常用的有以下幾種:

assert xx 判斷xx為真

assert not xx 判斷xx不為真

assert a in b 判斷b包含a

assert a == b 判斷a等於b

assert a != b 判斷a不等於b

四、裝飾器

標記表示式

pytest -m slow

將執行用**@ pytest.mark.slow**裝飾器修飾的所有測試。

import pytest

def add(x,y):

return x+y

def test_add_1(

): assert add(1,2)

[email protected]

def test_add_2(

): assert add(1,3)

==3

@pytest.mark.demo1

def test_passing(

): assert (1,2,3)

==(1,2,3)

上面的**塊中如果我執行

pytest -m demo1

則只會執行test_add_2()和test_passing(),其他的則不執行,如果在程式執行過程中你想執行不同檔案中的某些測試方法但是這些測試方法名字不一樣,你就可以通過裝飾器的方法進行標記。

skip

實際工作中可能會因為某些功能還沒去完全完成或者是bug還沒修復,可以在測試的時候可以選擇跳過這個test_case,這裡我們就可以用skip去完成。

跳過測試函式的最簡單方法是使用跳過裝飾器標記它,可以傳遞乙個可選的原因。

@pytest.mark.skip(reason=「本輪測試不執行此用例")

def test_the_unknown():

@pytest.mark.skip(reason=

'skip'

)def test_function(

): ..

..

skipif某些時候並不想所有的test_case都被跳過,想某種情況下才跳過,這裡的話就可以使用skipif來完成

import sys

@pytest.mark.skipif(sys.version_info <

(3,3),

reason=

"requires python3.3"

)def test_function(

): ...

可以在模組之間共享 skipif 標記。對於較大的測試套件,通常最好有乙個檔案來定義標記,然後一致適用於整個測試套件。下面是將測試

minversion = pytest.mark.skipif(5>3,reason=「test")

@minversion

def test_function(

): ...

共享之前需要先導入minversion

from test_mymodule import minversion
注意:使用pytest.skip(reason=』』),注意這個就不再是乙個裝飾器了,而僅僅是乙個函式了。此外需要注意的是,pytest.skip()放置位置對test case的執行至關重要。如果只放在乙個test case中,則僅僅會skip掉這乙個test case,比如:

import pytest

def test_123(

): pytest.skip(

"not implemented"

) assert 1 == 0

def test_234(

): assert 1 == 0

這裡會只跳過test_123()這個用例

如果把pytest_skip()放在了test_函式之外,則整個檔案下的test cases都會被skip掉而不被執行。如下所示:

import pytest

pytest.skip(

"not implemented"

)def test_123(

): assert 1 == 0

def test_234(

): assert 1 == 0

這裡所有的用例都會被跳

python pytest測試框架介紹三

之前介紹了pytest以xunit形式來寫用例,下面來介紹pytest特有的方式來寫用例 如下 這裡使用的了pytest的特有的模式來寫用例,使用的方式是scope方式,scope支援多種,後面會介紹 這裡還使用了pytest的addfinalizer內建功能,具體可參見官網,用處是 在最後乙個測試...

Python Pytest框架(三)測試韌體 下

一 執行命令pytest fixture 可以列出當前所有可用的fixture,包括內建的 外掛程式中的 以及當前專案定義的。eg conftest.py和test 1.py在同一目錄下,要使用測試韌體login。conftest.py import pytest pytest.fixture de...

Python pytest使用測試韌體的三種方式

conftest.py import pytest pytest.fixture name lg def login print 登入系統 test 1.py import pytest pytest.mark.usefixtures lg def test 1 conftest.py import...