pytest進階之fixture


前言

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

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'])

執行結果

test_fixture.py .我調用了fixtureFunc
                                                        [100%]

========================== 1 passed in 0.02 seconds ===========================
Process finished with exit code 0

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

fixture使用

調用fixture有三種方式

Fixture名字作為用例的參數

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'])

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

每個函數或者類前使用@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'])

使用autouse參數

指定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 ===========================

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

小結

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

fixtur作用范圍

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

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

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

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

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

4.function :前面實例已經說了,這個默認是默認的模式,函數級別的,每個測試用例執行前都會執行一次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里面執行使用 --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 ===========================
運行結果

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

fixture實現teardown

其實前面的所有實例都只是做了測試用例執行之前的准備工作,那么用例執行之后該如何實現環境的清理工作呢?這不得不說yield關鍵字了,相比大家都或多或少的知道這個關鍵字,他的作用其實和return差不多,也能夠返回數據給調用者,唯一的不同是被掉函數執行遇到yield會停止執行,接着執行調用處的函數,調用出的函數執行完后會繼續執行yield關鍵后面的代碼(具體原理可以看下我之前的文章關於生成器)。看下下面的實例來了解一下如何實現teardown功能

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'])

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

總結

1.fixture如何定義

2.fixture的使用方式

3.fixture作用范圍

4.fixture用yield實現teardown功能

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM