pytest--使用多个fixture和fixture直接互相调用

使用多个fixture

如果用例需要用到多个fixture的返回数据,fixture也可以return一个元组、list或字典,然后从里面取出对应数据。

# -*- coding=utf-8 -*-

import pytest
@pytest.fixture()
def user():
    a="admin"
    b="123456"
    return (a,b)
def test_t1(user):
    u=user[0]
    p=user[1]
    print("user类型{}".format(type(user))) #查看返回类型
    print("用户名:{},密码:{}".format(u,p))
    assert u=="admin"
if __name__ == "__main__":
    pytest.main(["-s", " test_001.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_001.py .user类型<class 'tuple'>
用户名:admin,密码:123456
                                                            [100%]

============================== 1 passed in 0.14s ==============================

当然也可以分开定义成多个fixture,然后test_用例传多个fixture参数

# -*- coding=utf-8 -*-

import pytest
@pytest.fixture()
def user():
    a = "admin"
    return a
@pytest.fixture()
def pwd():
    p = "888888"
    return p
def test_t1(user, pwd):
    u = user
    p = pwd
    print("user类型{}".format(type(user)))  # 查看返回类型
    print("用户名:{},密码:{}".format(u, p))
    assert p == '888888'
if __name__ == "__main__":
    pytest.main(["-s", " test_001.py"])

运行结果

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_001.py .user类型<class 'str'>
用户名:admin,密码:888888
                                                            [100%]

============================== 1 passed in 0.11s ==============================
fixture与fixture互相调用
import pytest
@pytest.fixture()
def user():
    a = "admin"
    return a
@pytest.fixture()
def pwd(user):
    p = "888888"
    return user, p
def test_t1(user, pwd):
    u = user
    p = pwd[1]
    print("pwd类型{}".format(type(pwd)))  # 查看返回类型
    print(f"pwd类型{pwd}") 
    print("用户名:{},密码:{}".format(u, p))
    assert p != None
if __name__ == "__main__":
    pytest.main(["-s", " test_001.py"])

运行结果:

============================= test session starts =============================
platform win32 -- Python 3.8.2, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: D:\study\xyautotest
plugins: allure-pytest-2.8.19, Faker-4.18.0, hypothesis-6.14.6, assume-2.4.3, forked-1.3.0, html-2.1.1, metadata-1.10.0, ordering-0.6, rerunfailures-9.1.1, xdist-2.3.0, tep-0.8.9collected 1 item

test_001.py .pwd类型<class 'tuple'>
pwd类型('admin', '888888')
用户名:admin,密码:888888
                                                            [100%]

============================== 1 passed in 0.11s ==============================
上一篇:golang 实现线程池


下一篇:添加几种常见docker的命令设置