Fork me on GitHub

pytest 学习 - 03 fixture

前言

有的场景setup与teardown无法满足,如:有的用例需要登录才能执行,有的用例不需要登录。

fixture

fixture可以满足以上特殊的场景。

1. 只有登录的函数上面加上 @pytest.fixture()

2. 在要使用的测试方法中传入(登录函数名称),就先登录

案例

import pytest

@pytest.fixture()
def login():
    print("登录")

def test_case1(login):
    print("测试用例1")
    pass

def test_case2():
    print("测试用例2")
    pass

if __name__ == '__main__':
    pytest.main(["-s"])

 

posted @ 2024-05-14 11:00  测栈云  阅读(3)  评论(0编辑  收藏  举报