python-pytest學習(三)-setup/teardown


一、簡介

  setup和teardown是每次用例開始前和結束后都去執行一次。

  更高級一點的,setupClass和teardownClass,需要配合@classmethod裝飾器一起使用,在做selenium自動化的時候,它的效率尤為突出,可以只啟動一次瀏覽器執行多個用例。

  pytest框架也有類似於setup和teardown的語法,並且還不只這四個。

二、用例運行級別

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

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

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

  4.方法級(setup_method/teardown_method)開始於方法始末(在類中);

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

三、函數級

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

  (1)代碼實現

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"])

  (2)運行結果

 

 

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

  (1)代碼實現

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_module.py"])

  (2)運行結果

 

   3.類和方法

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

  (2)代碼實現

import pytest

class TestClass:
    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_class.py"])

  (3)運行結果

 

 

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

 

  4.函數和類混合

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

  (2)代碼實現

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_f+c.py"])

  (3)運行結果

 

 

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

 

 

參考文章:https://mp.weixin.qq.com/s/basmOut7Lq2RLO2ICkmslA


免責聲明!

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



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