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 = "[email protected]"
    receivers = "[email protected]"

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

    # 連接郵箱服務器,qq郵箱和密碼,換成自己的
    username = "[email protected]"
    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 href='http://www.testingunion.com'>開源優測社區</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減少輸出信息:

2、setup_class(cls)

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

3、setup_module(module)

  • setup_module/teardown_module在一個模塊內,只會執行一次,作用於模塊內的所有用例
  • 示例中構建了2個class和1個def,共4個用例,可以看到,4個用例只執行了一次module

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": "[email protected]", 
        "receivers": "[email protected]",
        "smtpserver": "smtp.qq.com",
        "smtpport": 465,
        "username":"[email protected]",
        "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 href='http://www.testingunion.com'>開源優測社區</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的一致,就不粘代碼了。

總結

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外


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章