首页 > 其他分享 >Pytest - Fixture(9) - Fixture传参给测试用例

Pytest - Fixture(9) - Fixture传参给测试用例

时间:2023-05-30 18:11:14浏览次数:33  
标签:传参 __ name py Fixture pytest 测试用例 login

Pytest - Fixture传参给测试用例

  • 如果想要依赖fixture传递参数给测试用例,可以通过 yield 或者 return 来返回参数;

    • yield:实现setup和teardown,并将参数传递给测试用例;

    • return :仅实现setup,并将参数传递给测试用例而已;


传单个参数

  • return 传递单个参数:

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        name = "==我是账号=="
        # 返回参数
        return name
    
    
    # 使用函数传参
    def test_s1(login):
        print("\n**测试用例**")
    
        # 获取使用参数
        print(login)
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
    

    image-20230529093926332


  • yield 传递单个参数:

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        name = "==我是账号=="
        # 返回参数
        yield name
    
        print("^^测试结束^^")
    
    
    # 使用函数传参
    def test_s1(login):
        print("\n**测试用例**")
    
        # 返回的是一个元组
        print(login)
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
    

    image-20230529093818352



传多个参数

  • 如果测试用例想要依赖fixture读取测试数据文件,返回多组不同的数据,返回多个参数;

  • 测试用例获取的时候也用多个参数接受数据;

    test_py.py

    import pytest
    
    @pytest.fixture
    def login():
        name = "==我是账号=="
        pwd = "==我是密码=="
        age = "==我是年龄=="
    
        # 返回变量
        yield name, pwd, age
    
    
    # 使用函数传参
    def test_s1(login):
        print("\n==测试用例==")
    
    # 返回的是一个元组
    print(f"返回的是一个元组:{login}")
    
    # 分别赋值给不同变量
    name, pwd, age = login
    
    # 输出变量内容
    print("输出变量内容:" + name, pwd, age)
    
    if __name__ == '__main__':
        pytest.main(['-q', 'test_py.py'])
    

    image


标签:传参,__,name,py,Fixture,pytest,测试用例,login
From: https://www.cnblogs.com/mzline/p/17443935.html

相关文章

  • Pytest - Fixture(8) - 作用域依赖关系
    Pytest-作用域依赖关系添加了@pytest.fixture,如果fixture还想依赖其他fixture,需要用函数传参的方式:当一个函数请求另一个函数时,首先执行另一个函数。如果函数b请求函数a,函数a将首先执行,因为b依赖于a,没有a就无法运行。即使b不需要a的结果,它仍然可以......
  • Pytest - Fixture(7) - 作用域执行顺序
    Pytest-作用域执行顺序如果有多个、不同作用域的需要执行,要弄清楚它们将执行的顺序;pytest首先执行范围更高的fixtrue;在请求fixture函数中,先执行较高作用域范围的fixture(session,package,module);再执行在较低作用域的fixture(class,function);test_py.pyimportpyte......
  • 接口测试用例设计
    接口测试流程需求分析->测试设计->测试用例评审->测试执行->验收->预发布->上线接口测试用例设计思路梳理业务流程图,根据流程图中的分支分别考虑不同场景(包括接口超时,接口异常,接口请求成功,接口请求失败)基本功能流程测试冒烟测试验证主业务的正向流程测试正向......
  • Pytest - Fixture(6) - 作用域混用/执行顺序/依赖关系
    Pytest-Fixture作用域混用若测试用例调用多个不同级别的作用域,都会同时生效:conftest.pyimportpytest"""会话级别fixture,作用域当前目录"""@pytest.fixture(scope="session")deflogin_session():"""作用于整个py文件"""pr......
  • JavaScript函数传参原理详解——值传递还是引用传递
    讨论JavaScript的传参原理之前,我们先来看一段曾经让笔者困惑了一段时间的代码vartestA=1;vartestB={};functiontestNumber(example){example=2;}functiontestObj(example){example.test=1;}testNumber(testA);testObj(testB);console.log(testA);//......
  • 数组指针、二级指针传参
    voidtest(int**p){}//二级指针接受intmain(){ inta=0; int*p=&a; int**pp=&p; int*arr[10]={0}; test(pp);//二级指针传参 test(&p);//一级指针的地址 test(arr);//指针数组,存放指针地址的数组 return0;}//voidtest(intarr[][5])//{}//arr[][]arr[3][]错误......
  • Pytest - Fixture(5) - 作用域(scope)
    Pytest-Fixture作用域(scope)之前讲过,fixture里面有个scop参数,这个参数就是填写fixture作用域的;function:每一个函数或方法都会调用;class:每一个类调用一次,一个类中可以有多个方法;module:每一个.py文件调用一次,该文件内又有多个function和class;session:是多个文......
  • 函数之传参
    一、参数的两大分类1、形式参数 在'函数定义阶段'括号内依次写入的变量名就叫形式参数,简称"形参"defindex(a,b,c,d,e):pass#a,b就称之为是函数的形参2、实际参数 在'函数调用阶段'括号内依次传入的变量值就叫实际参数,简称"实参"index(1,2,3) #1,2,3就称之为是函......
  • Pytest - Fixture(3) - yield遇到异常
    Pytest-yield遇到异常如果yield前面的代码,即setup部分已经抛出异常了,则不会执行yield后面的teardown内容;如果测试用例抛出异常,yield后面的teardown内容还是会正常执行;test_py.pyimportpytest#配置自动运行的[email protected](scope="function",autouse=Tr......
  • vue3 router 路由传参
    路由跳转importrouterfrom"@/router";router.push({path:"/iframe",query:{url:frameurl.value}});获取参数importrouterfrom"@/router";import{useRoute}from"vue-router";constroute=useRoute();const......