python pytest測試框架介紹三

2022-04-04 11:12:44 字數 4180 閱讀 4040

之前介紹了pytest以xunit形式來寫用例,下面來介紹pytest特有的方式來寫用例

**如下

這裡使用的了pytest的特有的模式來寫用例,使用的方式是scope方式,scope支援多種,後面會介紹

這裡還使用了pytest的addfinalizer內建功能,具體可參見官網,用處是:在最後乙個測試專案中呼叫teardown

**如下

from

__future__

import

print_function

import

pytest

@pytest.fixture()

defresource_a():

print('

\nresources_a() "setup"')

deftest_1_that_needs_resource_a(resource_a):

print('

test_1_that_needs_resource_a()')

deftest_2_that_does_not():

print('

\ntest_2_that_does_not()')

deftest_3_that_does(resource_a):

print('

test_3_that_does()

')

這是最簡單的乙個例子,結果如下

可以看出測試用例繼承了用pytest.fixture的函式後,都執行了setup的功能,預設的pytest.fixture是function

在寫用例時,有幾種方法使用pytest.fixture來形成框架,

方法一:

就是上面提到的這種方法,如下

pytest.fixture()

defbefore():

print('

\nbefore each test')

deftest_1(before):

print('

test_1()')

deftest_2(before):

print('

test_2()

')

方法二:使用fixture修飾

@pytest.fixture()

defbefore():

print('

\nbefore each test')

@pytest.mark.usefixtures(

"before"

)def

test_1():

print('

test_1()')

@pytest.mark.usefixtures(

"before")

deftest_2():

print('

test_2()

')

標紅的就是修飾器

之前使用@pytest.fixture(scope='module')來定義框架,scope的引數有以下幾種

如下乙個用module例子

@pytest.fixture(scope='

module')

defresource_a():

print('

\nresources_a() "setup"')

deftest_1_that_needs_resource_a(resource_a):

print('

test_1_that_needs_resource_a()')

deftest_2_that_does_not():

print('

\ntest_2_that_does_not()')

deftest_3_that_does(resource_a):

print('

test_3_that_does()

')

即使我們在每個用例都繼承了resource_a,但在實際測試中,所有用例只執行了一次resource_a

這時,你可能會問,為什麼只這setup功能,沒有teardown功能,要teardown怎麼寫,方法如下:

def

cheese_db(request):

.....

defteardown():

print('

\n[teardown] cheese_db finalizer, disconnect from db')

request.addfinalizer(teardown)

這裡使用的還是之前介紹的request.addfinalizer功能,函式名字可以任意取,不一定要teardown

這裡就不介紹了,看官方文件吧

看**,如下

@pytest.fixture(scope="

module")

deffoo(request):

print('

\nfoo setup - module fixture')

deffin():

print('

foo teardown - module fixture')

request.addfinalizer(fin)

@pytest.fixture()

defbar(request):

print('

bar setup - function fixture')

deffin():

print('

bar teardown - function fixture')

request.addfinalizer(fin)

@pytest.fixture()

defbaz(request):

print('

baz setup - function fixture')

deffin():

print('

baz teardown - function fixture')

request.addfinalizer(fin)

deftest_one(foo, bar, baz):

print('

in test_one()')

deftest_two(foo, bar, baz):

print('

in test_two()

')

測試結果如下

pytest還有很有用的yield功能,後續再介紹

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

一 框架介紹及安裝 pytest是python的一種單元測試框架,與python自帶的unittest測試框架類似,但是比unittest框架使用起來更簡潔,效率更高。安裝 pip install pytest 我這裡已經安裝成功了 二 框架使用流程 建立如下的原始碼檔案test one.py de...

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...