學習-Pytest(五)yield操做

1.fixture的teardown操做並非獨立的函數,用yield關鍵字呼喚teardown操做

2.scope="module"

1.fixture參數scope=」module」,module做用是整個.py文件都會生效( 整個文件只會執行一次),python

用例調用時,參數寫上函數名稱就行瀏覽器

# 新建一個文件test_f1.py
# coding:utf-8
import pytest

@pytest.fixture(scope="module")
def open():
    print("打開瀏覽器,而且打開百度首頁")

def test_s1(open):
    print("用例1:搜索python-1")

def test_s2(open):  # 不傳login
    print("用例2:搜索python-2")

def test_s3(open):
    print("用例3:搜索python-3")

if __name__ == "__main__":
    pytest.main(["-s", "test_f1.py"])

運行結果:session

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

..\..\..\..\..\..\test_f1.py 打開瀏覽器,而且打開百度首頁
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================
View Code

從結果看出,雖然test_s1,test_s2,test_s3三個地方都調用了open函數,可是它只會在第一個用例前執行一次ide

2.若是test_s1不調用,test_s2(調用open),test_s3不調用函數

# 新建一個文件test_f1.py
# coding:utf-8
import pytest

@pytest.fixture(scope="module")
def open():
    print("打開瀏覽器,而且打開百度首頁")

def test_s1():
    print("用例1:搜索python-1")

def test_s2(open):  # 不傳login
    print("用例2:搜索python-2")

def test_s3():
    print("用例3:搜索python-3")

if __name__ == "__main__":
    pytest.main(["-s", "test_f1.py"])

運行結果:spa

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

..\..\..\..\..\..\test_f1.py 用例1:搜索python-1
.打開瀏覽器,而且打開百度首頁
用例2:搜索python-2
.用例3:搜索python-3
.

========================== 3 passed in 0.01 seconds ===========================
View Code

從結果看出,module級別的fixture在當前.py模塊裏,只會在用例(test_s2)第一次調用前執行一次code

3. yield執行teardown

1.fixture裏面的teardown用yield來喚醒teardown的執行orm

# 新建一個文件test_f1.py
# coding:utf-8
import pytest

@pytest.fixture(scope="module")
def open():
    print("打開瀏覽器,而且打開百度首頁")

    yield
    print("執行teardown!")
    print("最後關閉瀏覽器")

def test_s1(open):
    print("用例1:搜索python-1")

def test_s2(open):  # 不傳login
    print("用例2:搜索python-2")

def test_s3(open):
    print("用例3:搜索python-3")

if __name__ == "__main__":
    pytest.main(["-s", "test_f1.py"])

運行結果:blog

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

..\..\..\..\..\..\test_f1.py 打開瀏覽器,而且打開百度首頁
用例1:搜索python-1
.用例2:搜索python-2
.用例3:搜索python-3
.執行teardown!
最後關閉瀏覽器

========================== 3 passed in 0.01 seconds ===========================
View Code

3. yield遇到異常

1.若是其中一個用例出現異常,不影響yield後面的teardown執行,運行結果互不影響,而且在用例所有執行完以後,會呼喚teardown的內容utf-8

# 新建一個文件test_f1.py
# coding:utf-8
import pytest


@pytest.fixture(scope="module")
def open():
    print("打開瀏覽器,而且打開百度首頁")
    yield
    print("執行teardown!")
    print("最後關閉瀏覽器")

def test_s1(open):
    print("用例1:搜索python-1")

    # 若是第一個用例異常了,不影響其餘的用例執行
    raise NameError  # 模擬異常

def test_s2(open):  # 不傳login
    print("用例2:搜索python-2")

def test_s3(open):
    print("用例3:搜索python-3")

if __name__ == "__main__":
    pytest.main(["-s", "test_f1.py"])

運行結果:

test_f1.py 打開瀏覽器,而且打開百度首頁
用例1:搜索python-1
F
open = None

    def test_s1(open):
        print("用例1:搜索python-1")

        # 若是第一個用例異常了,不影響其餘的用例執行
>       raise NameError  # 模擬異常
E       NameError

D:\YOYO\test_f1.py:16: NameError
用例2:搜索python-2
.用例3:搜索python-3
.執行teardown!
最後關閉瀏覽器
View Code

2.若是在setup就異常了,那麼是不會去執行yield後面的teardown內容了

3.yield也能夠配合with語句使用,如下是官方文檔給的案例

# 官方文檔案例
# content of test_yield2.py

import smtplib
import pytest

@pytest.fixture(scope="module")
def smtp():
    with smtplib.SMTP("smtp.gmail.com") as smtp:
        yield smtp  # provide the fixture value
相關文章
相關標籤/搜索