pytest進(jìn)階之fixture

原文:pytest進(jìn)階之fixture - linux超 - 博客園 (cnblogs.com)

前言

學(xué)pytest就不得不說fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一樣,如果不學(xué)fixture那么使用pytest和使用unittest是沒什么區(qū)別的(個(gè)人理解)。

fixture用途

1.做測試前后的初始化設(shè)置,如測試數(shù)據(jù)準(zhǔn)備,鏈接數(shù)據(jù)庫,打開瀏覽器等這些操作都可以使用fixture來實(shí)現(xiàn)

2.測試用例的前置條件可以使用fixture實(shí)現(xiàn)

3.支持經(jīng)典的xunit fixture ,像unittest使用的setup和teardown

4.fixture可以實(shí)現(xiàn)unittest不能實(shí)現(xiàn)的功能,比如unittest中的測試用例和測試用例之間是無法傳遞參數(shù)和數(shù)據(jù)的,但是fixture卻可以解決這個(gè)問題

fixture定義

fixture通過@pytest.fixture()裝飾器裝飾一個(gè)函數(shù),那么這個(gè)函數(shù)就是一個(gè)fixture,看個(gè)實(shí)例

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"># test_fixture.py

import pytest

@pytest.fixture() def fixtureFunc(): return 'fixtureFunc'

def test_fixture(fixtureFunc): print('我調(diào)用了{(lán)}'.format(fixtureFunc)) if name=='main':
pytest.main(['-v', 'test_fixture.py'])</pre>

執(zhí)行結(jié)果

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;">test_fixture.py .我調(diào)用了fixtureFunc
[100%] ========================== 1 passed in 0.02 seconds =========================== Process finished with exit code 0</pre>

fixtureFunc 這個(gè)函數(shù)就是一個(gè)fixture,fixture函數(shù)內(nèi)部可以實(shí)現(xiàn)一些初始化操作!

fixture使用

調(diào)用fixture有三種方式

Fixture名字作為用例的參數(shù)

fixture的名字直接作為測試用例的參數(shù),上面的實(shí)例就這這種方式,再來看一個(gè)實(shí)例

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"># test_fixture.py

import pytest

@pytest.fixture() def fixtureFunc(): return 'fixtureFunc'

def test_fixture(fixtureFunc): print('我調(diào)用了{(lán)}'.format(fixtureFunc)) class TestFixture(object): def test_fixture_class(self, fixtureFunc): print('在類中使用fixture "{}"'.format(fixtureFunc)) if name=='main':
pytest.main(['-v', 'test_fixture.py'])</pre>

使用@pytest.mark.usefixtures('fixture')裝飾器

每個(gè)函數(shù)或者類前使用@pytest.mark.usefixtures('fixture')裝飾器裝飾

實(shí)例

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"># test_fixture.py
import pytest
@pytest.fixture() def fixtureFunc(): print('\n fixture->fixtureFunc')

@pytest.mark.usefixtures('fixtureFunc') def test_fixture(): print('in test_fixture')

@pytest.mark.usefixtures('fixtureFunc') class TestFixture(object): def test_fixture_class(self): print('in class with text_fixture_class') if name=='main':
pytest.main(['-v', 'test_fixture.py'])</pre>

使用autouse參數(shù)

指定fixture的參數(shù)autouse=True這樣每個(gè)測試用例會(huì)自動(dòng)調(diào)用fixture(其實(shí)這里說的不是很準(zhǔn)確,因?yàn)檫€涉及到fixture的作用范圍,那么我們這里默認(rèn)是函數(shù)級別的,后面會(huì)具體說fixture的作用范圍)

實(shí)例

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"># test_fixture.py
import pytest
@pytest.fixture(autouse=True) def fixtureFunc(): print('\n fixture->fixtureFunc') def test_fixture(): print('in test_fixture') class TestFixture(object): def test_fixture_class(self): print('in class with text_fixture_class') if name=='main':
pytest.main(['-v', 'test_fixture.py'])</pre>

結(jié)果

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"> fixture->fixtureFunc
.in test_fixture

fixture->fixtureFunc
.in class with text_fixture_class
[100%] ========================== 2 passed in 0.04 seconds ===========================</pre>

從結(jié)果可以看到每個(gè)測試用例執(zhí)行前都自動(dòng)執(zhí)行了fixture

小結(jié)

掌握上面的方法,就可以使用fixture了,那么這幾種方式又有是區(qū)別呢? 其實(shí)從我寫的代碼中就能看出來, 如果測試用例需要使用fixture中返回的參數(shù),那么通過后面這兩種方式是無法使用返回的參數(shù)的,因?yàn)閒ixture中返回的數(shù)據(jù)默認(rèn)存在fixture名字里面存儲(chǔ),所以只能使用第一種方式才可以調(diào)用fixture中的返回值。(理論永遠(yuǎn)是理論,看文章的老鐵還是自己試試吧!)

fixtur作用范圍

上面所有的實(shí)例默認(rèn)都是函數(shù)級別的,所以測試函數(shù)只要調(diào)用了fixture,那么在測試函數(shù)執(zhí)行前都會(huì)先指定fixture。說到作用范圍就不得不說fixture 的第二個(gè)參數(shù)scope參數(shù)。

scope參數(shù)可以是session, module,class,function; 默認(rèn)為function

1.session 會(huì)話級別(通常這個(gè)級別會(huì)結(jié)合conftest.py文件使用,所以后面說到conftest.py文件的時(shí)候再說)

2.module 模塊級別: 模塊里所有的用例執(zhí)行前執(zhí)行一次module級別的fixture

3.class 類級別 :每個(gè)類執(zhí)行前都會(huì)執(zhí)行一次class級別的fixture

4.function :前面實(shí)例已經(jīng)說了,這個(gè)默認(rèn)是默認(rèn)的模式,函數(shù)級別的,每個(gè)測試用例執(zhí)行前都會(huì)執(zhí)行一次function級別的fixture

下面我們通過一個(gè)實(shí)例具體看一下 fixture的作用范圍

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;"># test_fixture.py
import pytest

@pytest.fixture(scope='module', autouse=True) def module_fixture(): print('\n-----------------') print('我是module fixture') print('-----------------')
@pytest.fixture(scope='class') def class_fixture(): print('\n-----------------') print('我是class fixture') print('-------------------')
@pytest.fixture(scope='function', autouse=True) def func_fixture(): print('\n-----------------') print('我是function fixture') print('-------------------') def test_1(): print('\n 我是test1')

@pytest.mark.usefixtures('class_fixture') class TestFixture1(object): def test_2(self): print('\n我是class1里面的test2') def test_3(self): print('\n我是class1里面的test3')
@pytest.mark.usefixtures('class_fixture') class TestFixture2(object): def test_4(self): print('\n我是class2里面的test4') def test_5(self): print('\n我是class2里面的test5') if name=='main':
pytest.main(['-v', '--setup-show', 'test_fixture.py'])</pre>

運(yùn)行結(jié)果

我們在cdm里面執(zhí)行使用 --setup-show 可以查看到具體setup和teardoen順序

image

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;">test_fixture.py
SETUP M module_fixture
SETUP F func_fixture ----------------- 我是module fixture -----------------

----------------- 我是function fixture ------------------- test_fixture.py::test_1 (fixtures used: func_fixture, module_fixture).
我是test1

    TEARDOWN F func_fixture
  SETUP    C class_fixture
    SETUP    F func_fixture ----------------- 我是class fixture -------------------

----------------- 我是function fixture ------------------- test_fixture.py::TestFixture1::test_2 (fixtures used: class_fixture, func_fixture, module_fixture).
我是class1里面的test2

    TEARDOWN F func_fixture
    SETUP    F func_fixture ----------------- 我是function fixture ------------------- test_fixture.py::TestFixture1::test_3 (fixtures used: class_fixture, func_fixture, module_fixture).

我是class1里面的test3

    TEARDOWN F func_fixture
  TEARDOWN C class_fixture
  SETUP    C class_fixture
    SETUP    F func_fixture ----------------- 我是class fixture -------------------

----------------- 我是function fixture ------------------- test_fixture.py::TestFixture2::test_4 (fixtures used: class_fixture, func_fixture, module_fixture).
我是class2里面的test4

    TEARDOWN F func_fixture
    SETUP    F func_fixture ----------------- 我是function fixture ------------------- test_fixture.py::TestFixture2::test_5 (fixtures used: class_fixture, func_fixture, module_fixture).

我是class2里面的test5

    TEARDOWN F func_fixture
  TEARDOWN C class_fixture
TEARDOWN M module_fixture ========================== 5 passed in 0.05 seconds ===========================</pre>

我們可以很清楚的看到 整個(gè)模塊只執(zhí)行了一次module級別的fixture , 每個(gè)類分別執(zhí)行了一次class級別的fixture, 而每一個(gè)函數(shù)之前都執(zhí)行了一次function級別的fixture

fixture實(shí)現(xiàn)teardown

其實(shí)前面的所有實(shí)例都只是做了測試用例執(zhí)行之前的準(zhǔn)備工作,那么用例執(zhí)行之后該如何實(shí)現(xiàn)環(huán)境的清理工作呢?這不得不說yield關(guān)鍵字了,相比大家都或多或少的知道這個(gè)關(guān)鍵字,他的作用其實(shí)和return差不多,也能夠返回?cái)?shù)據(jù)給調(diào)用者,唯一的不同是被掉函數(shù)執(zhí)行遇到y(tǒng)ield會(huì)停止執(zhí)行,接著執(zhí)行調(diào)用處的函數(shù),調(diào)用出的函數(shù)執(zhí)行完后會(huì)繼續(xù)執(zhí)行yield關(guān)鍵后面的代碼(具體原理可以看下我之前的文章關(guān)于生成器)??聪孪旅娴膶?shí)例來了解一下如何實(shí)現(xiàn)teardown功能

<pre style="margin: 0px; padding: 0px; overflow: auto; font-family: Monaco, Menlo, Consolas, "Courier New", monospace; font-size: 12px; overflow-wrap: break-word; background: rgb(238, 238, 238); border: 0px; border-radius: 2px;">import pytest from selenium import webdriver import time
@pytest.fixture() def fixtureFunc():
  '''實(shí)現(xiàn)瀏覽器的打開和關(guān)閉'''
driver = webdriver.Firefox() yield driver
driver.quit() def test_search(fixtureFunc): '''訪問百度首頁,搜索pytest字符串是否在頁面源碼中''' driver = fixtureFunc
driver.get('http://www.baidu.com')
driver.find_element_by_id('kw').send_keys('pytest')
driver.find_element_by_id('su').click()
time.sleep(3)
source = driver.page_source assert 'pytest' in source if name=='main':
pytest.main(['--setup-show', 'test_fixture.py'])</pre>

這個(gè)實(shí)例會(huì)先打開瀏覽器,然后執(zhí)行測試用例,最后關(guān)閉瀏覽器。大家可以試試! 通過yield就實(shí)現(xiàn)了 用例執(zhí)行后的teardown功能

總結(jié)

1.fixture如何定義

2.fixture的使用方式

3.fixture作用范圍

4.fixture用yield實(shí)現(xiàn)teardown功能

最后提一句:實(shí)際工作中盡量少用auto=True這個(gè)參數(shù),可能會(huì)引發(fā)意想不到的結(jié)果! 最常用的還是通過傳遞參數(shù)最好!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容