關(guān)于pytest fixtures
,根據(jù)官方文檔介紹: fixture 用于提供一個(gè)固定的基線,使 Cases 可以在此基礎(chǔ)上可靠地、重復(fù)地執(zhí)行疙驾。對(duì)比 PyUnit 經(jīng)典的setup/teardown形式啥纸,它在以下方面有了明顯的改進(jìn):
- fixture擁有一個(gè)明確的名稱喜喂,通過聲明使其能夠在函數(shù)究珊、類、模塊纵苛,甚至整個(gè)測(cè)試會(huì)話中被激活使用剿涮;
- fixture以一種模塊化的方式實(shí)現(xiàn),原因在于每一個(gè)fixture的名字都能觸發(fā)一個(gè)fixture函數(shù)攻人,而這個(gè)函數(shù)本身又能調(diào)用其它的fixture取试;
- fixture的管理從簡(jiǎn)單的單元測(cè)試擴(kuò)展到復(fù)雜的功能測(cè)試,允許通過配置和組件選項(xiàng)參數(shù)化fixture和測(cè)試用例怀吻,或者跨功能瞬浓、類、模塊蓬坡,甚至整個(gè)測(cè)試會(huì)話復(fù)用fixture猿棉;
一句話概括:在整個(gè)測(cè)試執(zhí)行的上下文中,fixture
扮演注入者(injector)的角色屑咳,而測(cè)試用例扮演消費(fèi)者(client)的角色萨赁,測(cè)試用例可以輕松的接收和處理需要預(yù)初始化操作的應(yīng)用對(duì)象,而不用過分關(guān)心其實(shí)現(xiàn)的具體細(xì)節(jié)兆龙。
fixture的實(shí)例化順序
fixture支持的作用域(Scope):function(default)杖爽、class、module紫皇、package慰安、session。
其中聪铺,package作用域是在 pytest 3.7 的版本中化焕,正式引入的,目前仍處于實(shí)驗(yàn)性階段铃剔。
多個(gè)fixture的實(shí)例化順序锣杂,遵循以下原則:
- 高級(jí)別作用域的(例如:session)優(yōu)先于 低級(jí)別的作用域的(例如:class或者function)實(shí)例化;
- 相同級(jí)別作用域的番宁,其實(shí)例化順序遵循它們?cè)跍y(cè)試用例中被聲明的順序(也就是形參的順序)元莫,或者fixture之間的相互調(diào)用關(guān)系;
- 指明
autouse=True
的fixture蝶押,先于其同級(jí)別的其它fixture實(shí)例化踱蠢。
fixture 實(shí)現(xiàn) teardown 功能
有以下幾種方法:
注意:在yield之前或者addfinalizer注冊(cè)之前代碼發(fā)生錯(cuò)誤退出的,都不會(huì)再執(zhí)行后續(xù)的清理操作。
- 將fixture變?yōu)樯善鞣椒ǎㄍ扑])
即將fixture函數(shù)中的return關(guān)鍵字替換成yield茎截,則yield之后的代碼苇侵,就是我們要的清理操作。
@pytest.fixture(scope='session', autouse=True)
def clear_token():
yield
from libs.redis_m import RedisManager
rdm = RedisManager()
rdm.expire_token(seconds=60)
- 使用
addfinalizer
方法
fixture
函數(shù)能夠接收一個(gè)request
的參數(shù)企锌,表示測(cè)試請(qǐng)求的上下文(下面會(huì)詳細(xì)介紹)榆浓,我們可以使用request.addfinalizer
方法為fixture
添加清理函數(shù)。
@pytest.fixture()
def smtp_connection_fin(request):
smtp_connection = smtplib.SMTP("smtp.163.com", 25, timeout=5)
def fin():
smtp_connection.close()
request.addfinalizer(fin)
return smtp_connection
- 使用
with
寫法(不推薦)
對(duì)于支持with寫法的對(duì)象撕攒,我們也可以隱式的執(zhí)行它的清理操作:
@pytest.fixture()
def smtp_connection_yield():
with smtplib.SMTP("smtp.163.com", 25, timeout=5) as smtp_connection:
yield smtp_connection
fixture可以訪問測(cè)試請(qǐng)求的上下文
fixture函數(shù)可以接收一個(gè)request的參數(shù)陡鹃,表示測(cè)試用例、類抖坪、模塊萍鲸,甚至測(cè)試會(huì)話的上下文環(huán)境;
例如可以擴(kuò)展下上面的smtp_connection_yield
擦俐,讓其根據(jù)不同的測(cè)試模塊使用不同的服務(wù)器:
@pytest.fixture(scope='module')
def smtp_connection_request(request):
server, port = getattr(request.module, 'smtp_server', ("smtp.163.com", 25))
with smtplib.SMTP(server, port, timeout=5) as smtp_connection:
yield smtp_connection
print("斷開 %s:%d" % (server, port))
在測(cè)試模塊中指定smtp_server
:
smtp_server = ("mail.python.org", 587)
def test_163(smtp_connection_request):
response, _ = smtp_connection_request.ehlo()
assert response == 250
fixture返回工廠函數(shù)
如果需要在一個(gè)測(cè)試用例(function)中脊阴,多次使用同一個(gè)fixture實(shí)例,相對(duì)于直接返回?cái)?shù)據(jù)蚯瞧,更好的方法是返回一個(gè)產(chǎn)生數(shù)據(jù)的工廠函數(shù)嘿期。并且,對(duì)于工廠函數(shù)產(chǎn)生的數(shù)據(jù)埋合,也可以在fixture中對(duì)其管理:
@pytest.fixture
def make_customer_record():
# 記錄生產(chǎn)的數(shù)據(jù)
created_records = []
# 工廠
def _make_customer_record(name):
record = models.Customer(name=name, orders=[])
created_records.append(record)
return record
yield _make_customer_record
# 銷毀數(shù)據(jù)
for record in created_records:
record.destroy()
def test_customer_records(make_customer_record):
customer_1 = make_customer_record("Lisa")
customer_2 = make_customer_record("Mike")
customer_3 = make_customer_record("Meredith")
fixture的參數(shù)化
如果你需要在一系列的測(cè)試用例的執(zhí)行中秽五,每輪執(zhí)行都使用同一個(gè)fixture,但是有不同的依賴場(chǎng)景饥悴,那么可以考慮對(duì)fixture進(jìn)行參數(shù)化坦喘;這種方式適用于對(duì)多場(chǎng)景的功能模塊進(jìn)行詳盡的測(cè)試。
@pytest.fixture(scope='module', params=['smtp.163.com', "mail.python.org"])
def smtp_connection_params(request):
server = request.param
with smtplib.SMTP(server, 587, timeout=5) as smtp_connection:
yield smtp_connection
def test_parames(smtp_connection_params):
response, _ = smtp_connection_params.ehlo()
assert response == 250
在不同的層級(jí)上覆寫fixture
注意:低級(jí)別的作用域可以調(diào)用高級(jí)別的作用域西设,但是高級(jí)別的作用域調(diào)用低級(jí)別的作用域會(huì)返回一個(gè)ScopeMismatch的異常瓣铣。
在大型的測(cè)試中,可能需要在本地覆蓋項(xiàng)目級(jí)別的fixture贷揽,以增加可讀性和便于維護(hù):
@pytest.fixture(scope="module", autouse=True)
def init(frag_login):
pass
@pytest.fixture(scope='session')
def active_user_account(cmd_line_args, conf):
tail_num = cmd_line_args.get("tailnum", None)
if tail_num is None:
tail_num = "1"
for user in conf['unified']:
if str(user['uid'])[-1] == tail_num:
return user
msg = f"尾號(hào)[{tail_num}], 在配置文件中未找到"
logger.error(msg)
raise ValueError(msg)