pytest文檔23-使用多個fixture和fixture直接互相調用


使用多個fixture

如果用例需要用到多個fixture的返回數據,fixture也可以return一個元組、list或字典,然后從里面取出對應數據。

# test_fixture4.py
import pytest

@pytest.fixture()
def user():
    print("獲取用戶名")
    a = "yoyo"
    b = "123456"
    return (a, b)


def test_1(user):
    u = user[0]
    p = user[1]
    print("測試賬號:%s, 密碼:%s" % (u, p))
    assert u == "yoyo"

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

當然也可以分開定義成多個fixture,然后test_用例傳多個fixture參數

# test_fixture5.py
import pytest

@pytest.fixture()
def user():
    print("獲取用戶名")
    a = "yoyo"
    return a

@pytest.fixture()
def psw():
    print("獲取密碼")
    b = "123456"
    return b

def test_1(user, psw):
    '''傳多個fixture'''
    print("測試賬號:%s, 密碼:%s" % (user, psw))
    assert user == "yoyo"

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

fixture與fixture互相調用

fixture與fixture直接也能互相調用的

import pytest

@pytest.fixture()
def first():
    print("獲取用戶名")
    a = "yoyo"
    return a

@pytest.fixture()
def sencond(first):
    '''psw調用user fixture'''
    a = first
    b = "123456"
    return (a, b)

def test_1(sencond):
    '''用例傳fixture'''
    print("測試賬號:%s, 密碼:%s" % (sencond[0], sencond[1]))

    assert sencond[0] == "yoyo"

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

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

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

作者:上海-悠悠 QQ交流群:874033608

也可以關注下我的個人公眾號:yoyoketang


免責聲明!

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



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