美文网首页
84. pytest参数化驱动

84. pytest参数化驱动

作者: 薛东弗斯 | 来源:发表于2024-07-12 17:29 被阅读0次

    列表

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    
    def add(a,b):
        return a+b
    
    def data():
        return [
            [1,1,2],
            ['wuya','Share','wuyaShare'],
            [1.0,1.0,2.0]
        ]
    @pytest.mark.parametrize('a,b,result',data())
    def test_add_list(a,b,result):
        assert add(a=a,b=b) == result
    
    if __name__=='__main__':
        pytest.main(['-v',"main.py"])
    

    元组

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    
    def add(a,b):
        return a+b
    
    def data():
        return [
            (1,1,2),
            ('wuya','Share','wuyaShare'),
            (1.0,1.0,2.0)
        ]
    @pytest.mark.parametrize('a,b,result',data())  # 调用data函数,分别赋值给a,b,result
    def test_add_tuple(a,b,result):  # a,b,result作为参数传入
        assert add(a=a,b=b) == result
    
    if __name__=='__main__':
        pytest.main(['-v',"main.py"])
    

    字典

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    
    def add(a,b):
        return a+b
    
    def data():
        return [
            {'a':1,'b':1,'result':2},
            {'a':'wuya','b':'Share','result':'wuyaShare'},
            {'a':1.0,'b':1.0,'result':2.0}
        ]
    @pytest.mark.parametrize('data',data())  # 调用data函数,分别赋值给data
    def test_add_dict(data):  # data['a'],data['b'],data['result']作为参数传入
        assert add(a=data['a'],b=data['b']) == data['result']
    
    if __name__=='__main__':
        pytest.main(['-s','-v',"main.py"])
    

    JSON

    # add.json
    {
    "datas":
        [
            {"a":1,"b":1,"result":2},
            {"a":"wuya","b":"Share","result":"wuyaShare"},
            {"a":1.0,"b":1.0,"result":2.0}
        ]
    }
    
    # main.py
    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    import json
    
    def add(a,b):
        return a+b
    
    def readJson():
        return json.load(open('add.json'))['datas']
    
    @pytest.mark.parametrize('data',readJson())  # 调用data函数,分别赋值给data
    def test_add_dict(data):  # data['a'],data['b'],data['result']作为参数传入
        assert add(a=data['a'],b=data['b']) == data['result']
    
    if __name__=='__main__':
        pytest.main(['-s','-v',"main.py"])
    

    Yaml

    # add.yml
    ---
    # two integer add
    a: 1
    b: 1
    result: 2
    
    ---
    # two string add
    a: wuya
    b: Share
    result: wuyaShare
    
    ---
    # two float add
    a: 1.0
    b: 1.0
    result: 2.0
    
    # main.py
    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    import yaml
    
    def add(a,b):
        return a+b
    
    def readYaml():
        with open('add.yml') as f:
            return list(yaml.safe_load_all(f))
    
    @pytest.mark.parametrize('data',readYaml())  # 调用data函数,分别赋值给data
    def test_add_dict(data):  # data['a'],data['b'],data['result']作为参数传入
        assert add(a=data['a'],b=data['b']) == data['result']
    
    if __name__=='__main__':
        pytest.main(['-s','-v',"main.py"])
    

    request.param

    #! /usr/bin/env python
    # -*- coding:utf-8 -*-
    # author: Test
    
    import pytest
    
    def add(a,b):
        return a+b
    
    def data():
        return [
            [1,1,2],
            ['wuya','Share','wuyaShare'],
            [1.0,1.0,2.0]
        ]
    @pytest.fixture(params=data())
    def param(request):
        return request.param
    
    def test_add_request(param):
        assert add(a=param[0],b=param[1]) == param[2]
    
    if __name__=='__main__':
        pytest.main(['-v',"main.py"])
    

    相关文章

      网友评论

          本文标题:84. pytest参数化驱动

          本文链接:https://www.haomeiwen.com/subject/uajfhjtx.html