[接口測試_B] 06 Pytest的setup和teardown

pytest實際上是python自帶測試框架unittest的擴展,那么pytest是如何實現unittest中的setup和teardown的呢?

pytest初始化的類別和作用域

  • 模塊級別(Module level setup/teardown):作用于一個模塊內的所有class和def,對于所有class和def,setup和teardown只執行一次
def setup_module(module):
""" setup any state specific to the execution of the given module."""
def teardown_module(module):
""" teardown any state that was previously setup with a setup_module
method.
"""
  • 類級別(Class level setup/teardown):作用于一個class內中的所有test,所有用例只執行一次setup,當所有用例執行完成后,才會執行teardown
@classmethod
def setup_class(cls):
""" setup any state specific to the execution of the given class (which
usually contains tests).
"""
@classmethod
def teardown_class(cls):
 """ teardown any state that was previously setup with a call to
setup_class.
"""
  • 方法和函數級別(Method and function level setup/teardown):作用于單個測試用例,若用例沒有執行(如被skip了)或失敗了,則不會執行teardown
def setup_method(self, method):
""" setup any state tied to the execution of the given method in a
class. setup_method is invoked for every test method of a class.
"""
def teardown_method(self, method):
""" teardown any state that was previously setup with a setup_method
call.
"""

若用例直接寫在模塊中,而不是在類中,則用:

def setup_function(function):
""" setup any state tied to the execution of the given function.
Invoked for every test function in the module.
"""
def teardown_function(function):
""" teardown any state that was previously setup with a setup_function
call.
"""
  • pytest.fixture()裝飾函數,結合yield實現初始化和teardown
    舉個例子(pytest)文檔中的:
import smtplib
import pytest
@pytest.fixture(scope="module")
def smtp():
    smtp = smtplib.SMTP("smtp.gmail.com", 587, timeout=5)
    yield smtp # provide the fixture value
    print("teardown smtp")
    smtp.close()

運行結果:

$ pytest -s -q --tb=no
FFteardown smtp
2 failed in 0.12 seconds

pytest用例初始化操作的示例

為了體現初始化和環境恢復,本節演示采用郵件發送的腳本,可查看郵件發送的腳本:python發送郵件腳本或者參考文章:SMTP發送郵件

1、setup_method(self, method)

  • 在test_method.py中構建了3個測試用例,每個用例在執行前后都會執行setup_method/teardown_method連接smtp和斷開smtp。
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

import pytest

class TestSmtp():
    # 發件人和收件人,換成你自己的發件人、收件人qq號
    sender = "sender@qq.com"
    receivers = "rece@qq.com"

    # 郵箱服務器
    smtpserver = "smtp.qq.com"
    smtpport = 465

    # 連接郵箱服務器,qq郵箱和密碼,換成自己的
    username = "sendr@qq.com"
    password = "qq mail's password"
    smtp = smtplib.SMTP_SSL()

    def setup_method(self, method):
        self.smtp.connect(self.smtpserver, self.smtpport)
        self.smtp.login(self.username, self.password)
        print("成功登錄")

    def teardown_method(self, method):
        self.smtp.quit()
        print("斷開連接")

    def test_send_text(self):
        # 郵件發送、接收人員,郵件標題、正文
        msg = MIMEText("微信公眾號號:開源優測", "plain", "utf-8")
        msg["From"] = self.sender
        msg["To"] = self.receivers
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele_text", "utf-8")

        # 發送郵件
        self.smtp.sendmail(self.sender, self.receivers, msg.as_string())

    def test_send_html(self):
        msg = MIMEText("<p>微信公眾號號:開源優測</p><a , 
        "html", 
        "utf-8")
        msg["From"] = self.sender
        msg["To"] = self.receivers
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele_html", "utf-8")

        # 發送郵件
        self.smtp.sendmail(self.sender, self.receivers, msg.as_string())

    def test_send_attchment(self):
        # 郵件格式說明、發送、接收人員信息、郵件標題
        msg = MIMEMultipart()
        msg["From"] = self.sender
        msg["To"] = self.receivers
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele", "utf-8")

        # 構建帶附件的郵件正文
        msg.attach(MIMEText("微信公眾號:開源優測", "plain", "utf-8")) 
    
        # 構造附件,多個附件同理
        attach1 = MIMEText(open("judge_leap.json", 'rb').read(), "base64", "utf-8")
        attach1["Content-Type"] = "application/octet-stream"

        # 這里filename隨意寫,將會在郵件中顯示
        attach1["Content-Disposition"] = "attrachment;filename=code.py"
    
        # 關聯附件到正文
        msg.attach(attach1)
        # 發送郵件
        self.smtp.sendmail(self.sender, self.receivers, msg.as_string())


  • 查看結果,采用pytest -s -q 運行,-s 可以查看打印信息,-q減少輸出信息:


    test_method結果.png

2、setup_class(cls)

  • 作用于class的setup_class/teardown_class,類中所有的用例只會執行一次,如圖所示;
  • ps:用例與test_method.py的一致,參考上一串代碼。
test_class.png

3、setup_module(module)

  • setup_module/teardown_module在一個模塊內,只會執行一次,作用于模塊內的所有用例
  • 示例中構建了2個class和1個def,共4個用例,可以看到,4個用例只執行了一次module
test_module結果.png
test_module.py
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart

import pytest

info = {"sender": "your@qq.com", 
        "receivers": "yourother@qq.com",
        "smtpserver": "smtp.qq.com",
        "smtpport": 465,
        "username":"your@qq.com",
        "password": "yourpassword",
        "smtp": smtplib.SMTP_SSL()}


def setup_module(module):
    info["smtp"].connect(info["smtpserver"], info["smtpport"])
    info["smtp"].login(info["username"], info["password"])
    print("成功登錄")

def teardown_module(module):
    info["smtp"].quit()
    print("斷開連接")

class TestSendText():
     def test_send_text(self):
        # 郵件發送、接收人員,郵件標題、正文
        msg = MIMEText("微信公眾號號:開源優測", "plain", "utf-8")
        msg["From"] =info["sender"]
        msg["To"] = info["receivers"]
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele_text", "utf-8")

        # 發送郵件
        info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())
    
     def test_send_html(self):
        msg = MIMEText("<p>微信公眾號號:開源優測</p><a , 
        "html", 
        "utf-8")
        msg["From"] = info["sender"]
        msg["To"] = info["receivers"]
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele_html", "utf-8")

        # 發送郵件
        info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())

class TestSendAttach():
    def test_send_attchment(self):
        # 郵件格式說明、發送、接收人員信息、郵件標題
        msg = MIMEMultipart()
        msg["From"] = info["sender"]
        msg["To"] = info["receivers"]
        msg["Subject"] = Header("開源優測_DeepTest_from_chenlele", "utf-8")

        # 構建帶附件的郵件正文
        msg.attach(MIMEText("微信公眾號:開源優測", "plain", "utf-8")) 
    
        # 構造附件,多個附件同理
        attach1 = MIMEText(open("judge_leap.json", 'rb').read(), "base64", "utf-8")
        attach1["Content-Type"] = "application/octet-stream"

        # 這里filename隨意寫,將會在郵件中顯示
        attach1["Content-Disposition"] = "attrachment;filename=code.py"
    
        # 關聯附件到正文
        msg.attach(attach1)

        # 發送郵件
        info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())

def test_send_text_out():
    # 郵件發送、接收人員,郵件標題、正文
    msg = MIMEText("微信公眾號號:開源優測", "plain", "utf-8")
    msg["From"] =info["sender"]
    msg["To"] = info["receivers"]
    msg["Subject"] = Header("class外的用例執行", "utf-8")

    # 發送郵件
    info["smtp"].sendmail(info["sender"], info["receivers"], msg.as_string())

4、pytest.fixture()

  • pytest.fixture采用yield實現setup和teardown操作,yield提供的參數為函數名稱
  • 與setup_module類似,pytest.fixture可作用于一個模塊內的所有def和class。區別在于,必須將pytest.fixture()裝飾的函數作為參數傳遞給用例。
  • pytest.fixture()裝飾的函數必須作為參數傳遞給用例嗎?
    1)、將class中的smtp_ini都刪除,class中的用例執行失敗,def用例執行成功;
    2)、將class中test_send_text的smtp_ini保留,其余2個刪除,class中的用例都執行成功?這是為什么呢?只有1個用力傳入了參數,但所有用例都執行成功了。
    3)、將class和def中的smtp_ini都刪除,用例全部執行失敗。
  • ps:用例內容與test_module.py的一致,就不粘代碼了。
image.png

總結

4種方式的作用域:

  • setup_method:僅作用于class用例集中的用例,置于class內,每個用例都會調用一次
  • setup_function:作用于獨立的def用例,不可作用于class內的用例
  • setup_class:作用于class用例集中的用例,置于class內,只在class用例執行的開始執行setup_class,結束時執行teardown_class
  • setup_module:作用于模塊內的所有用例,置于class外,只在所以用例的開始執行setup_module,結束時執行teardown_module
  • pytest.fixture():作用于模塊內的所有用例,但需要傳遞裝飾函數為參數,可置于class內或class外
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 在pytest中加入fixture的目的是提供一個固定的基準,使測試能夠可靠、重復地執行,pytest的fixtu...
    何小有閱讀 13,587評論 1 17
  • Python 面向對象Python從設計之初就已經是一門面向對象的語言,正因為如此,在Python中創建一個類和對...
    順毛閱讀 4,241評論 4 16
  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,993評論 19 139
  • Startup 單元測試的核心價值在于兩點: 更加精確地定義某段代碼的作用,從而使代碼的耦合性更低 避免程序員寫出...
    wuwenxiang閱讀 10,163評論 1 27
  • 一個類的定義放在另一個類的內部,這個類就叫做內部類 10.1創建內部類 public class First {p...
    zlb閱讀 387評論 0 0