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.軟體測驗學習路線以及相對應的視頻學習教程免費分享!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/250485.html
標籤:其他
上一篇:Python版apollo客戶端
