1. 程式人生 > >pytest-23-使用多個fixture和fixture直接互相調用

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

測試 .py -c pre class () nco logs 用例

如果用例需要用到多個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-23-使用多個fixture和fixture直接互相調用