欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

python pytest進階之fixture詳解

系統(tǒng) 1790 0

前言

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

fixture用途

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

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

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

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

fixture定義

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

            
# 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 這個函數(shù)就是一個fixture,fixture函數(shù)內(nèi)部可以實現(xiàn)一些初始化操作!

fixture使用

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

方式1
fixture的名字直接作為測試用例的參數(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

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

實例

            
# 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這樣每個測試用例會自動調(diào)用fixture(其實這里說的不是很準(zhǔn)確,因為還涉及到fixture的作用范圍,那么我們這里默認(rèn)是函數(shù)級別的,后面會具體說fixture的作用范圍)

實例

            
# 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é)果可以看到每個測試用例執(zhí)行前都自動執(zhí)行了fixture

小結(jié)

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

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

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

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

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

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

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

下面我們通過一個實例具體看一下 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'])
          

運行結(jié)果

我們在cdm里面執(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 ===========================
          

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

fixture實現(xiàn)teardown

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

            
import pytest
from selenium import webdriver
import time
@pytest.fixture()
def fixtureFunc():
  '''實現(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'])
          

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

總結(jié)

1.fixture如何定義

2.fixture的使用方式

3.fixture作用范圍

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

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 欧美黄色大片在线观看 | 91福利精品老师国产自产在线 | 亚洲一区二区三区在线播放 | 亚洲天堂久久 | 国产高清一区二区 | 久草免费网站 | 超级成人97碰碰碰免费 | 国产精品a在线观看香蕉 | 亚洲人成网站999久久久综合 | 国产欧美一区二区久久 | 日韩免费在线观看视频 | 国产精品欧美一区二区三区 | 999久久久免费精品国产 | 精品一区二区三区免费毛片 | 亚洲国产一区在线 | 久久久激情视频 | 免费一区二区三区 | 在线观看www | 午夜视频在线观看免费视频 | 亚洲热在线观看 | 激情中文网 | 91短视频在线免费观看 | 超碰97av| 国产综合欧美 | 加勒比欧美 | 欧美大黄 | 久久精品国产免费 | 国产精品视频播放 | 毛片啪啪啪 | 国产一级免费视频 | 青青草国产 | 亚洲精品国精品久久99热 | 四虎综合 | 久久99热成人精品国产 | 成av在线 | 久久亚洲欧美日韩精品专区 | 日韩性freexxxx在线观看 | 欧美性生活久久 | 国产精品视频免费一区二区三区 | www.色婷婷| 色天天综合色天天碰 |