亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

python pytest進階之fixture詳解

 更新時間:2019年06月27日 10:42:00   作者:linux超  
這篇文章主要介紹了python pytest進階之fixture詳解,學pytest就不得不說fixture,fixture是pytest的精髓所在,就像unittest中的setup和teardown一樣,如果不學fixture那么使用pytest和使用unittest是沒什么區(qū)別的,需要的朋友可以參考下

前言

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

fixture用途

1.做測試前后的初始化設置,如測試數據準備,鏈接數據庫,打開瀏覽器等這些操作都可以使用fixture來實現

2.測試用例的前置條件可以使用fixture實現

3.支持經典的xunit fixture ,像unittest使用的setup和teardown

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

fixture定義

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

# test_fixture.py
import pytest
@pytest.fixture()
def fixtureFunc():
return 'fixtureFunc'
def test_fixture(fixtureFunc):
print('我調用了{}'.format(fixtureFunc))
if __name__=='__main__':
pytest.main(['-v', 'test_fixture.py'])

執(zhí)行結果

test_fixture.py .我調用了fixtureFunc
[100%]
========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

fixtureFunc 這個函數就是一個fixture,fixture函數內部可以實現一些初始化操作!

fixture使用

調用fixture有三種方式

方式1
fixture的名字直接作為測試用例的參數,上面的實例就這這種方式,再來看一個實例

# test_fixture.py
import pytest
@pytest.fixture()
def fixtureFunc():
return 'fixtureFunc'
def test_fixture(fixtureFunc):
print('我調用了{}'.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

每個函數或者類前使用@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的參數autouse=True這樣每個測試用例會自動調用fixture(其實這里說的不是很準確,因為還涉及到fixture的作用范圍,那么我們這里默認是函數級別的,后面會具體說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'])

結果

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

從結果可以看到每個測試用例執(zhí)行前都自動執(zhí)行了fixture

小結

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

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

scope參數可以是session, module,class,function; 默認為function

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

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

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

4.function :前面實例已經說了,這個默認是默認的模式,函數級別的,每個測試用例執(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'])

運行結果

我們在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, 而每一個函數之前都執(zhí)行了一次function級別的fixture

fixture實現teardown

其實前面的所有實例都只是做了測試用例執(zhí)行之前的準備工作,那么用例執(zhí)行之后該如何實現環(huán)境的清理工作呢?這不得不說yield關鍵字了,相比大家都或多或少的知道這個關鍵字,他的作用其實和return差不多,也能夠返回數據給調用者,唯一的不同是被掉函數執(zhí)行遇到yield會停止執(zhí)行,接著執(zhí)行調用處的函數,調用出的函數執(zhí)行完后會繼續(xù)執(zhí)行yield關鍵后面的代碼(具體原理可以看下我之前的文章關于生成器)??聪孪旅娴膶嵗齺砹私庖幌氯绾螌崿Fteardown功能

import pytest
from selenium import webdriver
import time
@pytest.fixture()
def fixtureFunc():
  '''實現瀏覽器的打開和關閉'''
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í)行測試用例,最后關閉瀏覽器。大家可以試試! 通過yield就實現了 用例執(zhí)行后的teardown功能

總結

1.fixture如何定義

2.fixture的使用方式

3.fixture作用范圍

4.fixture用yield實現teardown功能

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

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Python使用matplotlib和pandas實現的畫圖操作【經典示例】

    Python使用matplotlib和pandas實現的畫圖操作【經典示例】

    這篇文章主要介紹了Python使用matplotlib和pandas實現的畫圖操作,結合實例形式分析了Python基于matplotlib和pandas的數值運算與圖形顯示操作相關實現技巧,并對部分代碼的圖形顯示進行了顯示效果測試,需要的朋友可以參考下
    2018-06-06
  • Python 最強編輯器詳細使用指南(PyCharm )

    Python 最強編輯器詳細使用指南(PyCharm )

    這篇文章主要介紹了Python 最強編輯器詳細使用指南(PyCharm),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-09-09
  • Mac 上切換Python多版本

    Mac 上切換Python多版本

    Mac上自帶了Python2.x的版本,有時需要使用Python3.x版本做開發(fā),但不能刪了Python2.x,可能引起系統不穩(wěn)定,那么就需要安裝多個版本的Python下面通過本文給大家介紹Mac 上切換Python多版本的方法,需要的的朋友一起看看吧
    2017-06-06
  • Python異常處理之常見異常類型絕佳實踐詳解

    Python異常處理之常見異常類型絕佳實踐詳解

    這篇文章主要為大家介紹了Python異常處理之常見異常類型絕佳實踐詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-09-09
  • 使用python對文件中的數值進行累加的實例

    使用python對文件中的數值進行累加的實例

    今天小編就為大家分享一篇使用python對文件中的數值進行累加的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-11-11
  • pytorch獲取模型某一層參數名及參數值方式

    pytorch獲取模型某一層參數名及參數值方式

    今天小編就為大家分享一篇pytorch獲取模型某一層參數名及參數值方式,具有很好的價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • 使用Python的Tornado框架實現一個一對一聊天的程序

    使用Python的Tornado框架實現一個一對一聊天的程序

    這篇文章主要介紹了使用Python的Tornado框架實現一個一對一聊天的程序,程序基于WebSocket,需要的朋友可以參考下
    2015-04-04
  • PyQt5 closeEvent關閉事件退出提示框原理解析

    PyQt5 closeEvent關閉事件退出提示框原理解析

    這篇文章主要介紹了PyQt5 closeEvent關閉事件退出提示框原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-01-01
  • Python使用apscheduler模塊設置定時任務的實現

    Python使用apscheduler模塊設置定時任務的實現

    本文主要介紹了Python使用apscheduler模塊設置定時任務的實現,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • python 數據生成excel導出(xlwt,wlsxwrite)代碼實例

    python 數據生成excel導出(xlwt,wlsxwrite)代碼實例

    這篇文章主要介紹了python 數據生成excel導出(xlwt,wlsxwrite)代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-08-08

最新評論