zoukankan      html  css  js  c++  java
  • pytest---fixture参数化

    前言  

      前面介绍了,可以使用parametrize来做参数化,非常的方便,其实fixture也可以用来做参数化,灵活性更高。

    fixture参数化

    fixture前面介绍的时候说过一共有5个参数分别是:name,scope,params,autouse,ids。每个参数都会介绍到,今天主要介绍params参数,这个参数主要用来做fixture的参数化内容。

    这里需要用到一个参数request,用来接收fixture返回的结果。通过request.param来返回参数内容。

    import pytest
    
    data = ['anjing', 'test', 'admin']
    
    
    @pytest.fixture(params=data)
    def login(request):
        print('登录功能')
        yield request.param
        print('退出登录')
    
    
    class Test_01:
        def test_01(self, login):
            print('---用例01---')
            print('登录的用户名:%s' % login)
    
        def test_02(self):
            print('---用例02---')
    
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过执行结果可以发现fixture完成了参数化内容,有的小伙伴们可能会问了,如果是多个参数怎么用呢?这里安静在通过登陆案例在来介绍下多个参数

    fixture传入多个参数

    这里我们需要了解到通过fixture传参,是通过 request.param 进行整体接受到,如果参数多的话,需要request.param进行单独取出。

    import pytest
    
    data = [{'user': "anjing",
             'pwd': "123456",},
            {'user': "admin",
             "pwd": "123"},
            {'user':"test",
             'pwd': '1234'}]
    
    
    @pytest.fixture(params=data)
    def login(request):
        print('登录功能')
        yield request.param
        print('退出登录')
    
    class Test_01:
        def test_01(self, login):
            print('---用例01---')
            print('登录的用户名:%s' % login['user'])
            print('登录的密码:%s' % login['pwd'])
    
        def test_02(self):
            print('---用例02---')
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    安静这里就是通过把整体参数全部传出,然后通过request.param作为整体返回,返回的值就是通过login这个函数,然后通过login函数在进行取值,当然也可以在前置工作中进行直接取出来,然后传出时,直接调用

    自定义参数信息

    fixture的参数化自定义信息和parametrize是一样支持自定义信息的,都是通过ids参数来表示的。

    import pytest
    
    data = [{'user': "anjing",
             'pwd': "123456",},
            {'user': "admin",
             "pwd": "123"},
            {'user':"test",
             'pwd': '1234'}]
    
    
    @pytest.fixture(params=data, ids=['This is anjing','This is admin', 'This is test'])
    def login(request):
        print('登录功能')
        yield request.param
        print('退出登录')
    
    
    
    class Test_01:
        def test_01(self, login):
            print('---用例01---')
            print('登录的用户名:%s' % login['user'])
            print('登录的密码:%s' % login['pwd'])
    
        def test_02(self):
            print('---用例02---')
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    通过return返回参数

    上面介绍的是通过正常的request进行传参,也可以通过return直接进行返回参数,这里返回参数只能用来单个参数,不能使用参数化,只是简单介绍fixture的这个功能

    import pytest
    
    @pytest.fixture()
    def login():
        print('完成登录')
        a = {'user': "anjing",
             'pwd': "123456",}
        return a
    
    class Test_01:
        def test_01(self, login):
            print('---用例01---')
            print(login)
            
        def test_02(self):
            print('---用例02---')
    
    
    if __name__ == '__main__':
        pytest.main(['-vs'])

    简单的介绍了fixture的参数化内容,大家可以和parametrize进行做对比使用,具体哪个好用,大家可以进行使用那个。如果有更好的使用方法,可以下方留言进行交流哦

  • 相关阅读:
    判断Redis复制是否完成的方法
    jquery ajax 设置header的方式
    二维码 halcon(转)
    转 python 简易windows编辑器
    转 【ORACLE 12C】ORA-65066
    转 python 2 读取配置文件
    转 MySQL active threads more than 40 on db3.***.com
    推荐一个画连锁不平衡图(LD block)的软件LDBlockShow,亲测比haploview好用!
    ImportError: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.21' not found解决方法
    一个R包(IntAssoPlot),LocusZoom图、连锁不平衡图和基因结构图一步到位
  • 原文地址:https://www.cnblogs.com/qican/p/14231237.html
Copyright © 2011-2022 走看看