《帶你裝B,帶你飛》pytest修仙之路3 - setup/teardown


1. 簡介

學過unittest的都知道里面用前置和后置setup和teardown非常好用,在每次用例開始前和結束后都去執行一次。
當然還有更高級一點的setupClass和teardownClass,需配合@classmethod裝飾器一起使用,在做selenium自動化的時候,它的效率尤為突出,可以只啟動一次瀏覽器執行多個用例。
pytest框架也有類似於setup和teardown的語法,並且還不止這四個

2. 用例運行級別

  • 模塊級(setup_module/teardown_module)開始於模塊始末,全局的

  • 函數級(setup_function/teardown_function)只對函數用例生效(不在類中)

  • 類級(setup_class/teardown_class)只在類中前后運行一次(在類中)

  • 方法級(setup_method/teardown_method)開始於方法始末(在類中)

  • 類里面的(setup/teardown)運行在調用方法的前后

3. 函數式

3.1 setup_function/teardown_function (每個用例開始和結束時調用一次)

3.1.1 代碼實現:

3.1.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行

# 2.注釋:包括記錄創建時間,創建人,項目名稱。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《帶你裝B,帶你飛》pytest修仙之路3 - setup/teardown
'''
# 3.導入模塊
#  content of  test_bjhg_class1.py
import pytest
# 函數式


def setup_function():
    print("setup_function:每個用例開始前都會執行")

def teardown_function():
    print("teardown_function:每個用例結束后都會執行")

def test_one():
    print("正在執行----test_one")
    x = "this"
    assert 'h' in x

def test_two():
    print("正在執行----test_two")
    x = "hello"
    assert hasattr(x, 'check')

def test_three():
    print("正在執行----test_three")
    a = "hello"
    b = "hello world"
    assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_fixt.py"])
3.1.3運行結果:

運行代碼后,控制台打印如下圖的結果

3.2 setup_module/teardown_module(所有用例開始和結束時調用一次)

3.2.1 代碼實現:

3.2.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行

# 2.注釋:包括記錄創建時間,創建人,項目名稱。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《帶你裝B,帶你飛》pytest修仙之路3 - setup/teardown
'''
# 3.導入模塊
#  content of  test_bjhg_class1.py
import pytest
# 函數式

def setup_module():
    print("setup_module:整個.py模塊只執行一次")
    print("比如:所有用例開始前只打開一次瀏覽器")

def teardown_module():
    print("teardown_module:整個.py模塊只執行一次")
    print("比如:所有用例結束只最后關閉瀏覽器")

def setup_function():
    print("setup_function:每個用例開始前都會執行")

def teardown_function():
    print("teardown_function:每個用例結束前都會執行")

def test_one():
    print("正在執行----test_one")
    x = "this"
    assert 'h' in x

def test_two():
    print("正在執行----test_two")
    x = "hello"
    assert hasattr(x, 'check')

def test_three():
    print("正在執行----test_three")
    a = "hello"
    b = "hello world"
    assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_bjhg_class1.py"])
3.2.3 運行結果:

運行代碼后,控制台打印如下圖的結果

3.3 類和方法

1.setup/teardown和unittest里面的setup/teardown是一樣的功能,setup_class和teardown_class等價於unittest里面的setupClass和teardownClass

3.3.1 代碼實現:

3.3.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行

# 2.注釋:包括記錄創建時間,創建人,項目名稱。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《帶你裝B,帶你飛》pytest修仙之路3 - setup/teardown
'''
# 3.導入模塊
#  content of  test_bjhg_class1.py
import pytest
# 類和方法
class TestCase():

    def setup(self):
        print("setup: 每個用例開始前執行")

    def teardown(self):
        print("teardown: 每個用例結束后執行")

    def setup_class(self):
        print("setup_class:所有用例執行之前")

    def teardown_class(self):
        print("teardown_class:所有用例結束后執行")

    def setup_method(self):
        print("setup_method:  每個用例開始前執行")

    def teardown_method(self):
        print("teardown_method:  每個用例結束后執行")

    def test_one(self):
        print("正在執行----test_one")
        x = "this"
        assert 'h' in x

    def test_two(self):
        print("正在執行----test_two")
        x = "hello"
        assert hasattr(x, 'check')

    def test_three(self):
        print("正在執行----test_three")
        a = "hello"
        b = "hello world"
        assert a in b

if __name__ == "__main__":
    pytest.main(["-s", "test_bjhg_class1.py"])
3.3.3 運行結果:

運行代碼后,控制台打印如下圖的結果

備注:這里setup_method和teardown_method的功能和setup/teardown功能是一樣的,一般二者用其中一個即可

3.4 函數和類混合

1.如果一個.py的文件里面既有函數用例又有類和方法用例,運行順序又是怎樣的呢?

3.4.1 代碼實現:

3.4.2 參考代碼:
# coding=utf-8
# 1.先設置編碼,utf-8可支持中英文,如上,一般放在第一行

# 2.注釋:包括記錄創建時間,創建人,項目名稱。
'''
Created on 2020-1-07
@author: 北京-宏哥
Project:《帶你裝B,帶你飛》pytest修仙之路3 - setup/teardown
'''
# 3.導入模塊
#  content of  test_bjhg_class1.py
import pytest
# 類和方法
def setup_module():
    print("setup_module:整個.py模塊只執行一次")
    print("比如:所有用例開始前只打開一次瀏覽器")

def teardown_module():
    print("teardown_module:整個.py模塊只執行一次")
    print("比如:所有用例結束只最后關閉瀏覽器")

def setup_function():
    print("setup_function:每個用例開始前都會執行")

def teardown_function():
    print("teardown_function:每個用例結束前都會執行")

def test_one():
    print("正在執行----test_one")
    x = "this"
    assert 'h' in x

def test_two():
    print("正在執行----test_two")
    x = "hello"
    assert hasattr(x, 'check')

class TestCase():

    def setup_class(self):
        print("setup_class:所有用例執行之前")

    def teardown_class(self):
        print("teardown_class:所有用例執行之前")

    def test_three(self):
        print("正在執行----test_three")
        x = "this"
        assert 'h' in x

    def test_four(self):
        print("正在執行----test_four")
        x = "hello"
        assert hasattr(x, 'check')

if __name__ == "__main__":
    pytest.main(["-s", "test_bjhg_class1.py"])
3.4.3 運行結果:

運行代碼后,控制台打印如下圖的結果

2.從運行結果看出,setup_module/teardown_module的優先級是最大的,然后函數里面用到的setup_function/teardown_function與類里面的setup_class/teardown_class互不干涉

4. 小結

 好了,今天的分享就到這里吧!!!謝謝各位的耐心閱讀。有問題加群交流討論!!!

 

您的肯定就是我進步的動力。如果你感覺還不錯,就請鼓勵一下吧!記得隨手點波  推薦  不要忘記哦!!!

別忘了點 推薦 留下您來過的痕跡

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM