pytest文檔4-測試用例setup和teardown

前言

學過unittest的都知道里面用前置和後置setup和teardown很是好用,在每次用例開始前和結束後都去執行一次。
固然還有更高級一點的setupClass和teardownClass,需配合@classmethod裝飾器一塊兒使用,在作selenium自動化的時候,它的效率尤其突出,能夠只啓動一次瀏覽器執行多個用例。
pytest框架也有相似於setup和teardown的語法,而且還不止這四個瀏覽器

用例運行級別

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

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

  • 類級(setup_class/teardown_class)只在類中先後運行一次(在類中)函數

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

  • 類裏面的(setup/teardown)運行在調用方法的先後code

函數式

setup_function/teardown_function

1.pytest框架支持函數和類兩種用例方式,先看函數裏面的前置與後置用法:orm

setup_function/teardown_function 每一個用例開始和結束調用一次three

# test_fixt.py

# coding:utf-8
import pytest
# 函數式

** 做者:上海-悠悠 QQ交流羣:588402570**

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

運行結果:utf-8

============================= test session starts =============================
platform win32 -- Python 3.6.0, pytest-3.6.3, py-1.5.4, pluggy-0.6.0
rootdir: E:\YOYO, inifile:
collected 3 items

test_fixt.py setup_function:每一個用例開始前都會執行
正在執行----test_one
.teardown_function:每一個用例結束後都會執行
setup_function:每一個用例開始前都會執行
正在執行----test_two
Fteardown_function:每一個用例結束後都會執行
setup_function:每一個用例開始前都會執行
正在執行----test_three
.teardown_function:每一個用例結束後都會執行


================================== FAILURES ===================================
__________________________________ test_two ___________________________________

    def test_two():
        print("正在執行----test_two")
        x = "hello"
>       assert hasattr(x, 'check')
E       AssertionError: assert False
E        +  where False = hasattr('hello', 'check')

test_fixt.py:19: AssertionError
===================== 1 failed, 2 passed in 0.03 seconds ======================

2.從結果能夠看出用例執行順序:setup_function》用例1》teardown_function, setup_function》用例2》teardown_function, setup_function》用例3》teardown_functionget

備註:-s參數是爲了顯示用例的打印信息。 -q參數只顯示結果,不顯示過程

setup_function/teardown_function

1.setup_module是全部用例開始前只執行一次,teardown_module是全部用例結束後只執行一次

# test_fixt.py

# coding:utf-8
import pytest
# 函數式

** 做者:上海-悠悠 QQ交流羣:588402570**

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

2.從運行結果能夠看到setup_module和teardown_module只執行了一次

test_fixt.py setup_module:整個.py模塊只執行一次
好比:全部用例開始前只打開一次瀏覽器
setup_function:每一個用例開始前都會執行
正在執行----test_one
.teardown_function:每一個用例結束前都會執行
setup_function:每一個用例開始前都會執行
正在執行----test_two
Fteardown_function:每一個用例結束前都會執行
setup_function:每一個用例開始前都會執行
正在執行----test_three
.teardown_function:每一個用例結束前都會執行
teardown_module:整個.py模塊只執行一次
好比:全部用例結束只最好關閉瀏覽器

備註:setup_function/teardown_function和setup_module/teardown_module這四種方法是能夠任意組合的,用一個和多個均可以

類和方法

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

#test_fixtclass.py

# coding:utf-8
import pytest
# 類和方法

** 做者:上海-悠悠 QQ交流羣:588402570**

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

運行結果

test_fixtclass.py setup_class:全部用例執行以前
setup_method:  每一個用例開始前執行
setup: 每一個用例開始前執行
正在執行----test_one
.teardown: 每一個用例結束後執行
teardown_method:  每一個用例結束後執行
setup_method:  每一個用例開始前執行
setup: 每一個用例開始前執行
正在執行----test_two
Fteardown: 每一個用例結束後執行
teardown_method:  每一個用例結束後執行
setup_method:  每一個用例開始前執行
setup: 每一個用例開始前執行
正在執行----test_three
.teardown: 每一個用例結束後執行
teardown_method:  每一個用例結束後執行
teardown_class:全部用例執行以前

2.從結果看出,運行的優先級:setup_class》setup_method》setup 》用例》teardown》teardown_method》teardown_class
備註:這裏setup_method和teardown_method的功能和setup/teardown功能是同樣的,通常兩者用其中一個便可

函數和類混合

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

# coding:utf-8
import pytest
# 類和方法

** 做者:上海-悠悠 QQ交流羣:588402570**

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

運行結果:

test_fixtclass.py setup_module:整個.py模塊只執行一次
好比:全部用例開始前只打開一次瀏覽器
setup_function:每一個用例開始前都會執行
正在執行----test_one
.teardown_function:每一個用例結束前都會執行
setup_function:每一個用例開始前都會執行
正在執行----test_two
Fteardown_function:每一個用例結束前都會執行
setup_class:全部用例執行以前
正在執行----test_three
.正在執行----test_four
Fteardown_class:全部用例執行以前
teardown_module:整個.py模塊只執行一次
好比:全部用例結束只最後關閉瀏覽器

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

---------------------------------pytest結合selenium自動化完整版-------------------------

全書購買地址 https://yuedu.baidu.com/ebook/902224ab27fff705cc1755270722192e4536582b

做者:上海-悠悠 QQ交流羣:874033608

也能夠關注下個人我的公衆號:yoyoketang

相關文章
相關標籤/搜索