通过@pytest.fixture()装饰的函数或方法,方法名可以当做参数,传入到测试方法中;
作用:
1、可以在函数执行之前,完成特定的动作行为;(比如一些前置条件,动作)
2、可以返回数据给测试函数
使用场景:
1、方法A和方法C需要登录之后才能执行;
2、方法B不需要登录,就可以执行;
import pytest
# 登录接口
@pytest.fixture()
def login():
print('执行登录接口')
return ('cookie', 'pwd')
# 方法A
def test_A(login):
print('方法B,依赖登录')
# 方法C
def test_C():
print('方法C,不依赖登录')
if __name__ == '__main__':
pytest.main(["-q", "-s", "test_pytest.py"])
执行登录接口
方法B,依赖登录
.方法C,不依赖登录
.
2 passed in 0.04 seconds
Process finished with exit code 0
ending...