前言
學(xué)pytest就不得不說(shuō)fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一樣,如果不學(xué)fixture那么使用pytest和使用unittest是沒(méi)什么區(qū)別的(個(gè)人理解)。
fixture用途
1.做測(cè)試前后的初始化設(shè)置,如測(cè)試數(shù)據(jù)準(zhǔn)備,鏈接數(shù)據(jù)庫(kù),打開瀏覽器等這些操作都可以使用fixture來(lái)實(shí)現(xiàn)
2.測(cè)試用例的前置條件可以使用fixture實(shí)現(xiàn)
3.支持經(jīng)典的xunit fixture ,像unittest使用的setup和teardown
4.fixture可以實(shí)現(xiàn)unittest不能實(shí)現(xiàn)的功能,比如unittest中的測(cè)試用例和測(cè)試用例之間是無(wú)法傳遞參數(shù)和數(shù)據(jù)的,但是fixture卻可以解決這個(gè)問(wèn)題
fixture定義
fixture通過(guò)@pytest.fixture()裝飾器裝飾一個(gè)函數(shù),那么這個(gè)函數(shù)就是一個(gè)fixture,看個(gè)實(shí)例
# 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'])
執(zhí)行結(jié)果
test_fixture.py .我調(diào)用了fixtureFunc [100%] ========================== 1 passed in 0.02 seconds =========================== Process finished with exit code 0
fixtureFunc 這個(gè)函數(shù)就是一個(gè)fixture,fixture函數(shù)內(nèi)部可以實(shí)現(xiàn)一些初始化操作!
fixture使用
調(diào)用fixture有三種方式
方式1
fixture的名字直接作為測(cè)試用例的參數(shù),上面的實(shí)例就這這種方式,再來(lái)看一個(gè)實(shí)例
# 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'])
方式2
每個(gè)函數(shù)或者類前使用@pytest.mark.usefixtures('fixture')裝飾器裝飾
實(shí)例
# 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'])
方式3
指定fixture的參數(shù)autouse=True這樣每個(gè)測(cè)試用例會(huì)自動(dòng)調(diào)用fixture(其實(shí)這里說(shuō)的不是很準(zhǔn)確,因?yàn)檫€涉及到fixture的作用范圍,那么我們這里默認(rèn)是函數(shù)級(jí)別的,后面會(huì)具體說(shuō)fixture的作用范圍)
實(shí)例
# 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'])
結(jié)果
fixture->fixtureFunc .in test_fixture fixture->fixtureFunc .in class with text_fixture_class [100%] ========================== 2 passed in 0.04 seconds ===========================
從結(jié)果可以看到每個(gè)測(cè)試用例執(zhí)行前都自動(dòng)執(zhí)行了fixture
小結(jié)
掌握上面的方法,就可以使用fixture了,那么這幾種方式又有是區(qū)別呢? 其實(shí)從我寫的代碼中就能看出來(lái), 如果測(cè)試用例需要使用fixture中返回的參數(shù),那么通過(guò)后面這兩種方式是無(wú)法使用返回的參數(shù)的,因?yàn)閒ixture中返回的數(shù)據(jù)默認(rèn)存在fixture名字里面存儲(chǔ),所以只能使用第一種方式才可以調(diào)用fixture中的返回值。(理論永遠(yuǎn)是理論,看文章的老鐵還是自己試試吧!)
fixtur作用范圍
上面所有的實(shí)例默認(rèn)都是函數(shù)級(jí)別的,所以測(cè)試函數(shù)只要調(diào)用了fixture,那么在測(cè)試函數(shù)執(zhí)行前都會(huì)先指定fixture。說(shuō)到作用范圍就不得不說(shuō)fixture 的第二個(gè)參數(shù)scope參數(shù)。
scope參數(shù)可以是session, module,class,function; 默認(rèn)為function
1.session 會(huì)話級(jí)別(通常這個(gè)級(jí)別會(huì)結(jié)合conftest.py文件使用,所以后面說(shuō)到conftest.py文件的時(shí)候再說(shuō))
2.module 模塊級(jí)別: 模塊里所有的用例執(zhí)行前執(zhí)行一次module級(jí)別的fixture
3.class 類級(jí)別 :每個(gè)類執(zhí)行前都會(huì)執(zhí)行一次class級(jí)別的fixture
4.function :前面實(shí)例已經(jīng)說(shuō)了,這個(gè)默認(rèn)是默認(rèn)的模式,函數(shù)級(jí)別的,每個(gè)測(cè)試用例執(zhí)行前都會(huì)執(zhí)行一次function級(jí)別的fixture
下面我們通過(guò)一個(gè)實(shí)例具體看一下 fixture的作用范圍
# 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'])
運(yùn)行結(jié)果
我們?cè)赾dm里面執(zhí)行使用 --setup-show 可以查看到具體setup和teardoen順序
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 ===========================
我們可以很清楚的看到 整個(gè)模塊只執(zhí)行了一次module級(jí)別的fixture , 每個(gè)類分別執(zhí)行了一次class級(jí)別的fixture, 而每一個(gè)函數(shù)之前都執(zhí)行了一次function級(jí)別的fixture
fixture實(shí)現(xiàn)teardown
其實(shí)前面的所有實(shí)例都只是做了測(cè)試用例執(zhí)行之前的準(zhǔn)備工作,那么用例執(zhí)行之后該如何實(shí)現(xiàn)環(huán)境的清理工作呢?這不得不說(shuō)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í)例來(lái)了解一下如何實(shí)現(xiàn)teardown功能
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): '''訪問(wèn)百度首頁(yè),搜索pytest字符串是否在頁(yè)面源碼中''' 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'])
這個(gè)實(shí)例會(huì)先打開瀏覽器,然后執(zhí)行測(cè)試用例,最后關(guān)閉瀏覽器。大家可以試試! 通過(guò)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é)果! 最常用的還是通過(guò)傳遞參數(shù)最好!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
