W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
??fixture
??函數(shù)可以接受請(qǐng)求對(duì)象來(lái)內(nèi)省請(qǐng)求測(cè)試函數(shù)、類或模塊上下文。進(jìn)一步擴(kuò)展前面的??smtp_connection fixture?
?示例,讓我們從使用我們的??fixture
??的測(cè)試模塊中讀取一個(gè)可選的服務(wù)器URL:
# content of conftest.py
import pytest
import smtplib
@pytest.fixture(scope="module")
def smtp_connection(request):
server = getattr(request.module, "smtpserver", "smtp.gmail.com")
smtp_connection = smtplib.SMTP(server, 587, timeout=5)
yield smtp_connection
print("finalizing {} ({})".format(smtp_connection, server))
smtp_connection.close()
我們使用請(qǐng)求。模塊屬性可以獲得一個(gè)??smtpserver
??屬性的測(cè)試模塊。如果我們只是再次執(zhí)行,沒(méi)有什么變化:
$ pytest -s -q --tb=no test_module.py
FFfinalizing <smtplib.SMTP object at 0xdeadbeef0002> (smtp.gmail.com)
========================= short test summary info ==========================
FAILED test_module.py::test_ehlo - assert 0
FAILED test_module.py::test_noop - assert 0
2 failed in 0.12s
讓我們快速創(chuàng)建另一個(gè)測(cè)試模塊,在其模塊命名空間中實(shí)際設(shè)置服務(wù)器 URL:
# content of test_anothersmtp.py
smtpserver = "mail.python.org" # will be read by smtp fixture
def test_showhelo(smtp_connection):
assert 0, smtp_connection.helo()
運(yùn)行:
$ pytest -qq --tb=short test_anothersmtp.py
F [100%]
================================= FAILURES =================================
______________________________ test_showhelo _______________________________
test_anothersmtp.py:6: in test_showhelo
assert 0, smtp_connection.helo()
E AssertionError: (250, b'mail.python.org')
E assert 0
------------------------- Captured stdout teardown -------------------------
finalizing <smtplib.SMTP object at 0xdeadbeef0003> (mail.python.org)
========================= short test summary info ==========================
FAILED test_anothersmtp.py::test_showhelo - AssertionError: (250, b'mail....
上述示例??smtp_connection fixture
??函數(shù)從模塊名稱空間獲取我們的郵件服務(wù)器名稱。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: