柚子快報(bào)激活碼778899分享:后端 pytest
柚子快報(bào)激活碼778899分享:后端 pytest
fixture簡介及調(diào)用
閱讀目錄:
1.fixture的優(yōu)勢(shì)
2.fixture介紹
3.fixture的調(diào)用
4.fixture嵌套
fixture的優(yōu)勢(shì):
fixture可以根據(jù)需要自定義測(cè)試用例的前置、后置操作;fixture是通過yield來區(qū)分前后置的,前后置均可以單獨(dú)存在,fixture如果有后置,前置不報(bào)錯(cuò)就都會(huì)執(zhí)行,前置報(bào)錯(cuò)后置就不會(huì)執(zhí)行。與setup、teardown類似,fixture提供了測(cè)試執(zhí)行前和測(cè)試執(zhí)行后的處理,但是又比setup、teardown更靈活好用,比如:fixture命名更加靈活,不局限于setup和teardownconftest.py配置里可以實(shí)現(xiàn)數(shù)據(jù)共享,可以方便管理、修改和查看fixture函數(shù),并且不需要import就能自動(dòng)找到fixturefixture可用于封裝數(shù)據(jù),也可用于封邏輯動(dòng)作等
fixture使用介紹
簡介:
fixture裝飾器來標(biāo)記固定的功能函數(shù), 在其他函數(shù)、類、模塊或整個(gè)工程調(diào)用它時(shí),會(huì)被激活并優(yōu)先執(zhí)行,通常會(huì)被用于完成預(yù)置處理和重復(fù)操作。
方法:fixture(scope=“function”, params=None, autouse=False, ids= None, name=None)
常用參數(shù)
scope: 被@pytest.fixture標(biāo)記為方法的的作用域,默認(rèn)是function, 還可以是class、module、package、session等,(簡而言之 就是在需要前置后置后置的函數(shù))params: 用于給fixture傳參,可實(shí)現(xiàn)數(shù)據(jù)基于fixture的數(shù)據(jù)驅(qū)動(dòng),接收一個(gè)可以迭代的對(duì)象, 比如:列表[], 元組[], 字典列表{[],[],[]}、字典元組{(),(),()},提供參數(shù)數(shù)據(jù)供調(diào)用fixture的用例使用; 傳進(jìn)去的參數(shù),可以用request.param調(diào)用autouse: 是否自動(dòng)運(yùn)行,是一個(gè)bool值,默認(rèn)為false; 當(dāng)為True時(shí), 作用域內(nèi)的測(cè)試用例都會(huì)自動(dòng)調(diào)用該fixtureids: 用例標(biāo)識(shí)id,每一個(gè)ids 和 params 一一對(duì)應(yīng),如果沒有id,將從params自動(dòng)產(chǎn)生name: 給被@pytest.fixture標(biāo)記的方法取一個(gè)別名,如果使用了nam, 那只能將name傳入,函數(shù)名不再生效
fixture調(diào)用
函數(shù)引用/參數(shù)引用
方法:
將fixture名稱作為測(cè)試用例函數(shù)/方法的參數(shù);如果fixture有返回值,必需使用這種方式,否則獲取不到返回值(比如:`@pytest.mark.usefixtures()這種方式就獲取不到返回值,詳情見:后續(xù)填坑)函數(shù)引用: 測(cè)試類中測(cè)試方法形參是測(cè)試類外被@pytest.fixture()標(biāo)記的測(cè)試函數(shù),也就是說,fixture標(biāo)記的函數(shù)可以用于測(cè)試類 內(nèi)部參數(shù)引用: 測(cè)試類中測(cè)試方法性參數(shù)是當(dāng)前測(cè)試類中被@pytest.fixture()標(biāo)記的方法
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zcs
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun():
z = "fixture_fun函數(shù)"
print(" --- fixture ---")
assert 1 == 1
return z
@pytest.fixture()
def fun1():
z_1 = "fixture_fun1函數(shù)"
print(" --- fixture1 ---")
assert 2==2
return z_1
def test_a(fun):
print(" --- ------------------- test_a")
assert fun == "fixture_fun函數(shù)"
print("fun返回值判斷正確:%s"%fun)
class TestB(object):
def test_b(self, fun1):
print(" --------------------- test_b")
ass = 'AAAA'
if fun1 == ass:
print("返回的值正確")
else:
print("判斷的值是:%s"%ass)
print("fixture的返回值是:{}".format(fun1))
def test_c(self, fun3):
print(" --------------------- test_c")
@pytest.fixture()
def fun3(self):
print(" --------------------- test_d")
運(yùn)行結(jié)果:
fixture 返回params中的值
@pytest.fixture(params=['zzd', '接口自動(dòng)化測(cè)試', 'pytest測(cè)試大綱'])
def fun4(request): # 必須是 request這個(gè)參數(shù)名
return request.param # 依次取列表總的每一個(gè)值返回
class TestC(object):
def test_d(self, fun4):
print(" --------------------- test_d")
print(f"----------fun4, data={fun4}")
@pytest.fixture(params=[1,2,3,4,5,6,7,8,9,0])
def fun5(request):
return request.param * 10
def test_d(fun5):
print("--------------------- test_d")
print("傳入的值是:%s"%fun5)
運(yùn)行結(jié)果:
加裝飾器
測(cè)試用例上加裝飾器
可以多個(gè)fixture參數(shù),放前面的先執(zhí)行,放后面的后執(zhí)行,即 執(zhí)行順序和usefixtures后面引用順序?qū)?yīng)
示例一
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun():
print("---fixture")
@pytest.fixture()
def fun2():
print("---fixture2")
def test_a(fun):
print("--------------test_a")
class Test01:
def test_b(self, fun2):
print("--------------test_b")
@pytest.mark.usefixtures('fun2','fun')
def test_c(self):
print("--------------test_c")
運(yùn)行結(jié)果:
先調(diào)用fun_zsq_2 , 然后調(diào)用 fun_zsq_1
可以多個(gè)裝飾器,先執(zhí)行的放底層,后執(zhí)行的放上層 示例二:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun():
print("---fixture")
@pytest.fixture()
def fun2():
print("---fixture2")
def test_a(fun):
print("--------------test_a")
@pytest.mark.usefixtures('fun')
@pytest.mark.usefixtures('fun2')
class Test01:
def test_b(self):
print("--------------test_b")
def test_c(self):
print("--------------test_c")
運(yùn)行結(jié)果:
示例三:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun():
print("---fixture")
@pytest.fixture()
def fun2():
print("---fixture2")
def test_a(fun):
print("--------------test_a")
class Test01:
def test_b(self):
print("--------------test_b")
@pytest.mark.usefixtures('fun')
def test_c(self, fun2):
print("--------------test_c")
結(jié)果: 同時(shí)有裝飾器和引用,裝飾器先執(zhí)行
測(cè)試類上加裝飾器
類中所有測(cè)試用例都會(huì)調(diào)用該fixture
示例一:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun_zsq_1():
print("--------------------- 添加裝飾器")
@pytest.fixture()
def fun_zsq_2():
print("-------------------- 添加裝飾器2")
def test_zsq_1(fun_zsq_1):
print("-------------- test_zaq_1")
class Test001:
def test_zsq_2(self, fun_zsq_2):
print("--------------------- test_zaq_2")
@pytest.mark.usefixtures("fun_zsq_2", "fun_zsq_1")
def test_dd(self):
print("--------------------- test_dd")
結(jié)果: 同時(shí)有裝飾器和引用,裝飾器先執(zhí)行
示例二:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun_zsq_1():
print("--------------------- 添加裝飾器")
@pytest.fixture()
def fun_zsq_2():
print("-------------------- 添加裝飾器2")
@pytest.mark.usefixtures('fun_zsq_2')
class Test01:
def test_b(self):
print("--------------test_b")
@pytest.mark.usefixtures('fun_zsq_1')
def test_c(self):
print("--------------test_c")
結(jié)果: 方法和類上都有裝飾器,方法上裝飾器先執(zhí)行
總結(jié):
@pytest.mark.usefixtures('fun2')
@pytest.mark.usefixtures('fun')
等價(jià)于:
@pytest.mark.usefixtures('fun','fun2')
自動(dòng)適配: fixture 設(shè)置autouse=True
注意: 影響作用域內(nèi)所有用例
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def fun():
print("---fixture")
@pytest.fixture(autouse=True)
def fun2():
print("---fixture2")
def test_a():
print("--------------test_a")
class Test01:
def test_b(self):
print("--------------test_b")
def test_c(self):
print("--------------test_c")
結(jié)果:每個(gè)測(cè)試用例都執(zhí)行了fun_zd_1、fun_zd_2
fixture嵌套:
注意:不能用@pytest.mark.usefixtures 示例: 另個(gè)fixture, fun依賴 login
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author : zsc
# @wx :M_haynes
import pytest
@pytest.fixture()
def login():
print("---登錄")
@pytest.fixture()
def fun(login):
print("---fun")
# 下面寫法報(bào)錯(cuò)
# @pytest.fixture()
# @pytest.mark.usefixtures(login)
# def fun():
# print("---fun")
# @pytest.mark.usefixtures(fun) # 報(bào)錯(cuò)
def test_a(fun):
print("--------------test_a")
結(jié)果:
本文由mdnice多平臺(tái)發(fā)布
柚子快報(bào)激活碼778899分享:后端 pytest
精彩文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。