pytest fixture:使用fixture实现用例之间的调用

pytest fixture

定义fixture跟定义普通的函数差不多,唯一的区别就是在函数上加个装饰器@pytest.fixture()

fixture命名不要以test开头,跟用例区别开。fixture是有函数返回值的,没有返回值默认None

用例调用fixture的返回值,直接就是吧fixture的函数名称当做变量名称

import pytest

@pytest.fixture()
def init():
    print('init..')
    return 1

def test1(init):
    print('test1')

def test2(init):
    print('test2')

if __name__ == '__main__':
    pytest.main(['-vs','test06.py'])
原文地址:https://www.cnblogs.com/yronl/p/14463862.html