Pytest系列(4) - fixture的詳細使用

若是你還想從頭學起Pytest,能夠看看這個系列的文章哦!html

https://www.cnblogs.com/poloyy/category/1690628.htmlpython

 

前言

  • 前面一篇講了setup、teardown能夠實如今執行用例前或結束後加入一些操做,但這種都是針對整個腳本全局生效的
  • 若是有如下場景:用例 1 須要先登陸,用例 2 不須要登陸,用例 3 須要先登陸。很顯然沒法用 setup 和 teardown 來實現了
  • fixture可讓咱們自定義測試用例的前置條件

 

fixture的優點

  • 命名方式靈活,不侷限於 setup 和teardown 這幾個命名
  • conftest.py 配置裏能夠實現數據共享,不須要 import 就能自動找到fixture
  • scope="module" 能夠實現多個.py 跨文件共享前置
  • scope="session" 以實現多個.py 跨文件使用一個 session 來完成多個用例
 

fixture參數列表

@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
def test():
    print("fixture初始化的參數列表")

參數列表

  • scope:能夠理解成fixture的做用域,默認:function,還有class、module、package、session四個【經常使用】
  • autouse:默認:False,須要用例手動調用該fixture;若是是True,全部做用域內的測試用例都會自動調用該fixture
  • name:默認:裝飾器的名稱,同一模塊的fixture相互調用建議寫個不一樣的name

注意

session的做用域:是整個測試會話,即開始執行pytest到結束測試瀏覽器

 

測試用例如何調用fixture

  1. 將fixture名稱做爲測試用例函數的輸入參數
  2. 測試用例加上裝飾器:@pytest.mark.usefixtures(fixture_name)
  3. fixture設置autouse=True
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 15:50
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest

# 調用方式一
@pytest.fixture
def login():
    print("輸入帳號,密碼先登陸")


def test_s1(login):
    print("用例 1:登陸以後其它動做 111")


def test_s2():  # 不傳 login
    print("用例 2:不須要登陸,操做 222")


# 調用方式二
@pytest.fixture
def login2():
    print("please輸入帳號,密碼先登陸")


@pytest.mark.usefixtures("login2", "login")
def test_s11():
    print("用例 11:登陸以後其它動做 111")


# 調用方式三
@pytest.fixture(autouse=True)
def login3():
    print("====auto===")


# 不是test開頭,加了裝飾器也不會執行fixture
@pytest.mark.usefixtures("login2")
def loginss():
    print(123)

執行結果

 

fixture的實例化順序

  • 較高 scope 範圍的fixture(session在較低 scope 範圍的fixture( function 、 class 以前實例化【session > package > module > class > function】
  • 具備相同做用域的fixture遵循測試函數中聲明的順序,並遵循fixture之間的依賴關係【在fixture_A裏面依賴的fixture_B優先實例化,而後到fixture_A實例化】
  • 自動使用(autouse=True)的fixture將在顯式使用(傳參或裝飾器)的fixture以前實例化
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 16:14
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""
import pytest

order = []

@pytest.fixture(scope="session")
def s1():
    order.append("s1")


@pytest.fixture(scope="module")
def m1():
    order.append("m1")


@pytest.fixture
def f1(f3, a1):
    # 先實例化f3, 再實例化a1, 最後實例化f1
    order.append("f1")
    assert f3 == 123


@pytest.fixture
def f3():
    order.append("f3")
    a = 123
    yield a


@pytest.fixture
def a1():
    order.append("a1")


@pytest.fixture
def f2():
    order.append("f2")


def test_order(f1, m1, f2, s1):
    # m一、s1在f1後,但由於scope範圍大,因此會優先實例化
    assert order == ["s1", "m1", "f3", "a1", "f1", "f2"]

執行結果

斷言成功session

 

關於fixture的注意點

添加了 @pytest.fixture ,若是fixture還想依賴其餘fixture,須要用函數傳參的方式,不能用 @pytest.mark.usefixtures() 的方式,不然會不生效app

@pytest.fixture(scope="session")
def open():
    print("===打開瀏覽器===")

@pytest.fixture
# @pytest.mark.usefixtures("open") 不可取!!!不生效!!!
def login(open):
    # 方法級別前置操做setup
    print(f"輸入帳號,密碼先登陸{open}")

 

前面講的,其實都是setup的操做,那麼如今就來說下teardown是怎麼實現的ide

 

fixture之yield實現teardown

用fixture實現teardown並非一個獨立的函數,而是用 yield 關鍵字來開啓teardown操做函數

#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
__title__  =
__Time__   = 2020-04-06 15:50
__Author__ = 小菠蘿測試筆記
__Blog__   = https://www.cnblogs.com/poloyy/
"""

import pytest


@pytest.fixture(scope="session")
def open():
    # 會話前置操做setup
    print("===打開瀏覽器===")
    test = "測試變量是否返回"
    yield test
    # 會話後置操做teardown
    print("==關閉瀏覽器==")


@pytest.fixture
def login(open):
    # 方法級別前置操做setup
    print(f"輸入帳號,密碼先登陸{open}")
    name = "==我是帳號=="
    pwd = "==我是密碼=="
    age = "==我是年齡=="
    # 返回變量
    yield name, pwd, age
    # 方法級別後置操做teardown
    print("登陸成功")


def test_s1(login):
    print("==用例1==")
    # 返回的是一個元組
    print(login)
    # 分別賦值給不一樣變量
    name, pwd, age = login
    print(name, pwd, age)
    assert "帳號" in name
    assert "密碼" in pwd
    assert "年齡" in age


def test_s2(login):
    print("==用例2==")
    print(login)

yield注意事項

  • 若是yield前面的代碼,即setup部分已經拋出異常了,則不會執行yield後面的teardown內容
  • 若是測試用例拋出異常,yield後面的teardown內容仍是會正常執行

 

yield+with的結合

# 官方例子
@pytest.fixture(scope="module")
def smtp_connection():
    with smtplib.SMTP("smtp.gmail.com", 587, timeout=5) as smtp_connection:
        yield smtp_connection  # provide the fixture value

該 smtp_connection 鏈接將測試完成執行後已經關閉,由於 smtp_connection 對象自動關閉時, with 語句結束。測試

 

addfinalizer 終結函數

@pytest.fixture(scope="module")
def test_addfinalizer(request):
    # 前置操做setup
    print("==再次打開瀏覽器==")
    test = "test_addfinalizer"

    def fin():
        # 後置操做teardown
        print("==再次關閉瀏覽器==")

    request.addfinalizer(fin)
    # 返回前置操做的變量
    return test


def test_anthor(test_addfinalizer):
    print("==最新用例==", test_addfinalizer)

注意事項

  • 若是 request.addfinalizer() 前面的代碼,即setup部分已經拋出異常了,則不會執行 request.addfinalizer() 的teardown內容(和yield類似,應該是最近新版本改爲一致了)
  • 能夠聲明多個終結函數並調用
相關文章
相關標籤/搜索