码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-01-26 17:06:04      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:gpo   blog   多个   def   tle   word   meta   调用   com   

如果用例需要用到多个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直接互相调用

标签:gpo   blog   多个   def   tle   word   meta   调用   com   

原文地址:https://www.cnblogs.com/jason89/p/10323753.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!