http://doc.pytest.org/en/latest/fixture.html
fixture的優(yōu)點(diǎn)
顯式霞势、模塊化吱涉、可擴(kuò)展
- fixture具有明確的名稱榨呆,并通過從測試功能看疗,模塊,類或整個(gè)項(xiàng)目中聲明它們的使用來激活益涧。
- fixture以模塊化方式實(shí)現(xiàn),因?yàn)槊總€(gè)fixture名稱觸發(fā)fixture功能驯鳖,該fixture功能本身可以使用其他fixture闲询。
- fixture管理從簡單的單元擴(kuò)展到復(fù)雜的功能測試,允許根據(jù)配置和組件選項(xiàng)對夾具和測試進(jìn)行參數(shù)化浅辙,或者在功能扭弧,類,模塊或整個(gè)測試會話范圍內(nèi)重復(fù)使用夾具记舆。
fixture作為函數(shù)參數(shù)
import pytest
@pytest.fixture(scope="session")
def start():
print("fixture-start開始")
def test_3(start):
print("結(jié)束")
執(zhí)行結(jié)果:我們可以發(fā)現(xiàn)test_3被執(zhí)行前 先執(zhí)行了start方法
執(zhí)行邏輯:
在
@pytest.fixture(scope="session")
中我們我們寫有scope="session"鸽捻,那么這個(gè)參數(shù)的作用是什么呢?這個(gè)我們在后面的【fixture的執(zhí)行順序控制】中講到
fixture的模塊化
在使用中如果發(fā)現(xiàn)需要使用多個(gè)文件中的fixture泽腮,則可以將fixture寫入conftest.py中御蒲。
使用時(shí)不需要導(dǎo)入要在測試中使用的fixture,它會自動被pytest發(fā)現(xiàn)诊赊。
查找順序:
fixture功能的發(fā)現(xiàn)從測試類開始厚满,然后是測試模塊,然后是 conftest.py文件碧磅,最后是內(nèi)置和第三方插件
#conftest.py
import pytest
@pytest.fixture(scope="session")
def start():
print("fixture-start開始")
#test_fixtures2.py
def test_3(start):
print("結(jié)束")
執(zhí)行結(jié)果一致
可以使用
pytest --fixtures test_fixtures2.py
查看可用的fixture
------------------- fixtures defined from test.conftest --------------------
topics
conftest.py:11: no docstring available
start [session scope]
conftest.py:7: no docstring available
===================== no tests ran in 0.02 seconds =========================
fixture的執(zhí)行順序控制
前面我們提到在@pytest.fixture(scope="session")
中的scope參數(shù)碘箍,scope的作用是有多個(gè)fixture時(shí)指定優(yōu)先級
scope從低到高優(yōu)先級:function(默認(rèn))、class鲸郊、module丰榴、package(實(shí)驗(yàn),有風(fēng)險(xiǎn))严望、session多艇。
import pytest
# fixtures documentation order example
order = []
@pytest.fixture(scope="session")
def s1():
order.append("s1")
@pytest.fixture(scope="module")
def m1():
order.append("m1")
@pytest.fixture(scope="class")
def c1():
order.append("c1")
@pytest.fixture()
def f3():
order.append("f3")
class TestA():
def test_order(self, c1, s1, m1,f3):
print(order)
assert order == ["s1", "m1", "c1", "f3"]
執(zhí)行結(jié)果:我們可以發(fā)現(xiàn)執(zhí)行順序取決于fixture的scope傳參設(shè)置默認(rèn)執(zhí)行的fixture
對于所有test_方法都要使用的fixture像吻,我們在定義fixture時(shí)可以使用autouse=True
峻黍,就可以不傳參了
@pytest.fixture()
def f3():
order.append("f3")
@pytest.fixture(autouse=True) #設(shè)置的默認(rèn)執(zhí)行
def a1():
order.append("a1")
def test_1(f3):
print(order)
assert order == ["a1", "f3"]
執(zhí)行結(jié)果:我們在test_1方法中沒有傳a1复隆,但仍然執(zhí)行了fixture的嵌套
如果一個(gè)fixture執(zhí)行前需要先執(zhí)行另一個(gè)fixture,我們可以使用嵌套來解決
@pytest.fixture
def f1(f3):
order.append("f1")
@pytest.fixture()
def f3():
order.append("f3")
def test_1(f1):
print(order)
assert order == ["f3", "f1"]
執(zhí)行結(jié)果:fixture做后置處理yield
在pytest中我們有teardown_module之類的后置處理姆涩,fixture也可以支持相關(guān)操作
import pytest
@pytest.fixture()
def f1():
print("f1")
yield f1
print("f1 后置處理")
class TestA():
def test_1(self,f1):
print("test_1")
assert 2 == 1
可以看見在test_1()執(zhí)行后挽拂,執(zhí)行了yield
后面的語句。且無論用例是否執(zhí)行成功
- 注意:如果我們使用有
scope="class"
的fixture骨饿,則會在類執(zhí)行完成后進(jìn)行后置處理亏栈,其他的scope參數(shù)也一樣
通過request.module
獲取請求的module屬性
@pytest.fixture(scope="module")
def smtp(request):
server = getattr(request.module, "smtpserver")
print(server)
yield smtp
pass
smtpserver = "mail.python.org"
class TestA():
def test_showhelo(self,smtp):
pass
執(zhí)行結(jié)果:fixture通過request.module
將smtpserver取了出來
官網(wǎng)說還可以將類屬性取出來,但沒找到關(guān)鍵字
fixture的參數(shù)化
需要使用params
參數(shù)進(jìn)行參數(shù)化,然后通過request.param
取出
- 單個(gè)參數(shù)的參數(shù)化
@pytest.fixture(params=['a', 'v', 'c'])
def fix(request):
return request.param
def test_9(fix):
print(fix)
執(zhí)行結(jié)果:將三個(gè)參數(shù)依次取出- 多個(gè)參數(shù)的參數(shù)化----使用list
list = [{'a': '阿斯頓', 'b': '請問'},{'a': '自行車', 'b': '出現(xiàn)在'}]
@pytest.fixture(params=list)
def dic(request):
return request.param
def test_2(dic):
print('a=' + dic["a"])
print('b=' + dic["b"])
執(zhí)行結(jié)果:另外fixture還支持mock
http://www.reibang.com/p/4bd41d6bb2a9