@pytest.mark.parametrize如何实现argnames动态生成

@pytest.mark.parametrize("trendsfuns1,case", [('value1', 'value2'), ('value3', 'value4')], indirect=True)
def test_fixture_param_and_parametrize(trendsfuns1, case):
    print(trendsfuns1, case)

求指导,如何可以让此代码中的trendsfuns1。 这种数据的变量名实现动态的。

  • 这篇博客: 11、Pytest之@pytest.mark.parametrize使用详解中的 1、argnames、argvalues 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • '''
    @Author     : 测试工程师Jane
    @FileName   : parametrizetest.py
    @Description:
    '''
    import pytest
    
    @pytest.mark.parametrize('arg',[1])
    #测试函数要将argnames做为形参传入
    def test_one_params(arg):
        print("传入的值为:{}".format(arg))
        assert arg == 1
    

    在这里插入图片描述

    1.单参数多值,argvalues可以传入多样的python数据类型:列表,嵌套了元组的列表,字典,字符串
    2.传入多个值时,测试用例会被执行多次,每次取一个值去运行

    '''
    @Author     : 测试工程师Jane
    @FileName   : parametrizetest.py
    @Description:
    '''
    import pytest
    
    @pytest.mark.parametrize('arg',['abc',1,{'a':1,'b':3},(4,5)]
    def test_one_params(arg):
        print("传入的值为:{}".format(arg))
        assert isinstance(arg,dict)
    

    运行结果
    在这里插入图片描述

    从以上运行结果可以看出,当传入多个值时,测试用例会被执行多次,每次取一个值去运行,并断言。

    '''
    @Author     : 测试工程师Jane
    @FileName   : parametrizetest.py
    @Description:
    '''
    import pytest
    
    @pytest.mark.parametrize("test_input,expected",[("3+5",8),("5-2",1),("5*2",10)])
    def test_params(test_input,expected):
        print("原值:{} 期望值{}".format(test_input,expected))
        assert eval(test_input) == expected
    

    运行结果:
    在这里插入图片描述