測試開發全棧之Python自動化 Pytest 之 fixture

Pytest 之 fixture

  • unittest 和 nose 都支持 fixture 的,但是 fixture 在 pytest 裏使用更靈活。也算是 pytest 的一個閃光點吧
  • 可以理解爲一個跟 setup 和 teardown 這種前後置類似的東西。但是比它們要強大、靈活很多

fixtur 當做參數傳入

# -*- coding: utf-8 -*-

import pytest

@pytest.fixture()
def login():
    print('登錄系統')

# 直接使用函數名做爲參數傳入
def test_01(login):
    print('測試用例一')

def test_02():
    print('測試用例2')

def test03():
    print('測試用例3')

運行結果

  • 只有 tes_01 調用了 login
  • 遺留問題來了,如果我這裏有 10 個方法或更多?是不是都需調用 login 方法?繼續看下面的 fixture 參數
testcase.py::test_01 登錄系統
測試用例一
PASSED
testcase.py::test_02 測試用例2
PASSED
testcase.py::test03 測試用例3
PASSED

fixture 語法

# scope有4個作用範圍:function(不填則默認)、class、module、session
fixture(scope='function', params=None, autouse=False, ids=None, name=None)

參數說明

  • scope:即作用域,function"(默認),"class","module","session"四個
  • params:可選參數列表,它將導致多個參數調用 fixture 函數和所有測試使用它。
  • autouse:默認:False,需要用例手動調用該 fixture;如果是 True,所有作用域內的測試用例都會自動調用該 fixture
  • ids:params 測試 ID 的一部分。如果沒有將從 params 自動生成.
  • name:默認:裝飾器的名稱,同一模塊的 fixture 相互調用建議寫個不同的 name。
  • session 的作用域:是整個測試會話,即開始執行 pytest 到結束測試 scope 參數作用範圍控制 fixture 的作用範圍:session>module>class>function

autouse

  • 參數置默認爲 False,則需要手動去調用裝飾器
# -*- coding: utf-8 -*-

import pytest

# 當前就算定義了裝飾器,也不會調用Login
@pytest.fixture()
def login():
    print("打開瀏覽器")

def test1():
    print("test1裏的用例")

def test2():
    print("test2裏的用例")

調用方式 1

# -*- coding: utf-8 -*-

import pytest

@pytest.fixture()
def login():
    print("打開瀏覽器")

# 直接傳入函數名
def test1(login):
    print("test1裏的用例")

def test2(login):
    print("test2裏的用例")

調用方式 2

# -*- coding: utf-8 -*-

import pytest

# autouse設爲True,就能自動調用login的裝飾器
@pytest.fixture(autouse=True)
def login():
    print("打開瀏覽器")

# 直接傳入函數名
def test1():
    print("test1裏的用例")

def test2():
    print("test2裏的用例")

function

  • function:作用域爲函數
  • 所有的方法都調用了 login
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture(scope='function', autouse=True)
def login():
    print('登錄系統')

def test_01():
    print('測試用例一')

def test_02():
    print('測試用例2')

def test03():
    print('測試用例3')

運行結果

  • 符合用例名設計的都會調用裝飾器
  • login 不符合所以不會調用
testcase.py::test_01 登錄系統
測試用例一
PASSED
testcase.py::test_02 登錄系統
測試用例2
PASSED
testcase.py::test03 登錄系統
測試用例3
PASSED

class

  • class:作用域爲類
  • 所以 TestCase1 和 TestCase2 這兩個類都會執行 login
# -*- coding: utf-8 -*-

# @Time : 2021/1/14 21:05
# @Author : 公衆號(程序員一凡)

import pytest

@pytest.fixture(scope='class', autouse=True)
def login():
    print('登錄系統')


def test_01():
    print('這個是類外面的用例')

class TestCase1:
    def test_02(self):
        print('測試用例2')
    def test03(self):
        print('測試用例3')

class TestCase2:
    def test_04(self):
        print('測試用例4')
    def test05(self):
        print('測試用例5')

運行結果

  • 類裏面的方法只會調用一次

  • pytest 機制,因爲方法是以 test 開頭,所以也會調用

testcase.py::test_01 登錄系統
這個是類外面的用例
PASSED
testcase.py::TestCase1::test_02 登錄系統
測試用例2
PASSED
testcase.py::TestCase1::test03 測試用例3
PASSED
testcase.py::TestCase2::test_04 登錄系統
測試用例4
PASSED
testcase.py::TestCase2::test05 測試用例5
PASSED

module

  • module:在當前.py 腳本里面所有用例開始前只執行一次
  • 只要符合用例的設計要求,不管是類裏和外邊的都會調用
# -*- coding: utf-8 -*-

import pytest

@pytest.fixture(scope='class', autouse=True)
def open():
    print("打開瀏覽器,並且打開百度首頁")

def test_s1():
    print("用例1:搜索python-1")

class TestCase():
    def test_s2(self):
        print("用例2:搜索python-2")

    def test_s3(self):
        print("用例3:搜索python-3")

運行結果

  • 當前文件裏的用例都調用了裝飾器
  • 如果類名不是爲 Test 開頭你試試看是否還會調用裝飾器?
testcase.py::test_s1 打開瀏覽器並且打開百度首頁
用例1搜索python-1
PASSED
testcase.py::TestCase::test_s2 打開瀏覽器並且打開百度首頁
用例2搜索python-2
PASSED
testcase.py::TestCase::test_s3 用例3搜索python-3
PASSED

session

  • fixture 爲 session 級別是可以跨.py 模塊調用的
  • 當我們有多個.py 文件的用例時候,如果多個用例只需調用一次 fixture,那就可以設置爲 scope="session",並寫到 conftest.py 文件裏
  • conftest.py 文件名稱是固定的,pytest 會自動識別該文件。放到工程的根目錄下,就可以全局調用了
  • 如果放到某個 package 包下,那就只在該 package 內有效
# -*- coding: utf-8 -*-

# conftest文件內容
import pytest

@pytest.fixture(scope="session", autouse=True)
def login():
    print("調用conftest文件的裏的方法")

兩個用例文件

# -*- coding: utf-8 -*-
# testcase1.py 

import pytest

def test1():
    print("test1裏的用例")

def test2():
    print("test2裏的用例")

# -*- coding: utf-8 -*-
# testcase1.py  
import pytest
def test3():
    print("test3裏的用例")

def test4():
    print("test4裏的用例")

運行結果

  • 兩個文件只有 testcase 文件的用例調了 conftest 裏的方法
testcase.py::test1 調用conftest文件的裏的方法
test1裏的用例
PASSED
testcase.py::test2 test2裏的用例
PASSED
testcase1.py::test3 test3裏的用例
PASSED
testcase1.py::test4 test4裏的用例
PASSED

pytest-allure 生成測試報告

03


未來的你肯定會感謝現在拼命的自己!

給大家推薦一個軟件測試技術交流羣:1079636098  羣友福利免費領取

願你我相遇,皆有所獲! 歡迎關注微信公衆號:程序員一凡 

1.免費領取一份216頁軟件測試工程師面試寶典文檔資料。 

2.軟件測試學習路線以及相對應的視頻學習教程免費分享!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章