# pytest夹具的继承
继承的规则和执行顺序
- 夹具在继承的时候,被继承的夹具范围要大于等于继承夹具
- 执行顺序(如下图)
![]()
例
点击查看代码
import pytest
@pytest.fixture
def fixture1():
print("< 11111111111")
yield 1 # 方法的输出
print("111111111 >")
@pytest.fixture
def fixture2(fixture1): # 继承fixture1的返回
fixture1 += 1 # 夹具1的返回值做处理
print("< 2222222")
yield fixture1
print("222222 >")
def test_something(fixture2):
print(fixture2)
# 夹具在继承的时候,被继承的夹具范围要大于等于继承夹具
if __name__ == '__main__':
pytest.main(["-s", "夹具的继承.py"])
返回结果


浙公网安备 33010602011771号