pytest简易教程(09):fixture返回值(实现参数化)

全栈测试笔记 / 2024-02-24 / 原文

 

pytest简易教程汇总,详见

特点

1. fixture可以通过设计params,让依赖该fixture的用例迭代执行

2. params数据可以为[列表],(元组),{集合},{字典}

3. params数据在fixture中通过request变量来接收

 

示例:fixture返回值

  

结果:

 

如果fixture有返回值,用@pytest.mark.usefixtures()报错,无法获取到返回值

@pytest.mark.usefixtures(fun)
class TestX:
    def test_case(self):
        print("---test_case")
        print(f"data={fun}")

  

结果:

 

示例:fixture返回params中的值

  

结果:

 

示例:也可以yield返回

  

结果:

 

笛卡尔积

应用场景:需要传多个参数的不同组合,比如注册接口

 

结果: