前言:前面我們已經(jīng)對pytest有個大致的了解了滋饲,現(xiàn)在我們開始深入的挖掘它一些其他功能吧先改!
三、進階
A)玩轉(zhuǎn)裝飾器@pytest.fixture
主要作用:通常會被用于完成預(yù)置處理和重復(fù)操作朵诫。
@pytest.fixture(scope="function", params=None, autouse=False, ids=None, name=None)
常用參數(shù):scope:被標記方法的作用域
function" (default):作用于每個測試方法,每個test都運行一次
"class":作用于整個類毅哗,每個class的所有test只運行一次
"module":作用于整個模塊,每個module的所有test只運行一次
"session:作用于整個session捧挺,每個session只運行一次
params:(list類型)提供參數(shù)數(shù)據(jù)虑绵,供調(diào)用標記方法的函數(shù)使用
autouse:是否自動運行,默認為False不運行寝凌,設(shè)置為True自動運行
case1:@pytest.fixture(autouse=True)
import pytest
@pytest.fixture(autouse=True)
def must():
print("======》first & must run")
class TestCase:
def setup_class(self):
print("=======>setup class")
def teardown_class(self):
print("=======>teardown class")
def test_one(self):
assert 0 == 1
def test_twp(self):
assert 1 == 1
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
結(jié)果如下:
test_report.py =======>setup class
======》first & must run
F======》first & must run
.=======>teardown class
你嘗試下會發(fā)現(xiàn)標志了@pytest.fixture(autouse=True)會在每個測試函數(shù)運行前運行裕偿,且運行優(yōu)先級高于setup/teardown函數(shù)而低于setup_class/teardown_class函數(shù)诚些。
case2:@pytest.fixture() # 參數(shù)引用
import pytest
class TestCase:
@pytest.fixture()
def para(self):
print('=====>para func')
return 1
def test_one(self, para): # 以變量形式傳入
a = para + 1
assert a == 2
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
運行結(jié)果如下:
test_report.py =====>para func
.
========================== 1 passed in 0.05 seconds ===========================
同時邓尤,被@pytest.fixture標記的函數(shù)也可以作返回值、函數(shù)引用... ...
case3:@pytest.fixture(scope="function")
import pytest
@pytest.fixture(scope='function', autouse=True)
def para():
print('=====>para func')
class TestCase:
def test_one(self):
assert 1 == 2
def test_two(self):
assert 2 == 2
def test_para():
assert 0 != 0
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
運行結(jié)果如下:
test_report.py =====>para func
F=====>para func
.=====>para func
F
建議大家使用上面的代碼切換scope限制不同的區(qū)域渴肉,體驗scope的效果。
case4:@pytest.fixture(params=[1,2,3])
import pytest
@pytest.fixture(params=[1, 2, 3]) # 注意此處必須是可迭代的參數(shù)
def data(request):
print("=====>data")
return request.param
class TestCas:
def test_one(self, data):
print("=====>test_one")
assert data > 1
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
運行結(jié)果為:
test_report.py =====>data
=====>test_one
F=====>data
=====>test_one
.=====>data
=====>test_one
.
進階二、跳過待測試函數(shù)
主要作用:跳過標記的測試cases摊溶。
import pytest
a = 2
class TestCase:
@pytest.mark.skip('跳過測試')
def test_one(self):
assert 1 == 2
@pytest.mark.skipif(condition=a > 1, reason='若condition成立則跳過測試')
def test_two(self):
assert 2 == 2
def test_para(self):
assert 0 != 1
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
運行結(jié)果為:
test_report.py ss.
進階三、標記為預(yù)期失敗的測試用例
主要作用:預(yù)先標記測試用例為失敗的充石,方便局部執(zhí)行莫换。
import pytest
a = 2
class TestCase:
@pytest.mark.xfail(condition=a > 1, reason='若condition成立則跳過測試')
def test_two(self):
assert 2 == 2
def test_para(self):
assert 0 != 1
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
運行結(jié)果為:
test_report.py X.
進階四、測試函數(shù)添加待測參數(shù)
主要作用:為待測試的case迭代多個參數(shù)骤铃,減少多數(shù)測試case的重復(fù)編寫拉岁。
import pytest
class TestCase:
@pytest.mark.parametrize('a', [1, 2])
def test_two(self, a):
print("====test data: %s" % a)
assert 2 == 2
def test_para(self):
assert 0 != 1
if __name__ == '__main__':
pytest.main(['-s', 'test_report.py'])
執(zhí)行結(jié)果如下:
test_report.py ====test data: 1
.====test data: 2
.
總結(jié)
好了,pytest庫就暫告一段落惰爬,接著我要開始重構(gòu)我的測試框架了喊暖。期間時間可能稍微久一點,我還是會繼續(xù)學(xué)習(xí)的~
加油K呵啤A赀础!