调用函数,在装饰器内随机化测试参数

时间:2017-09-30 07:20:29

标签: python loops testing decorator

我正在编写各种测试。 他们中的大多数看起来像这样:

    @pytest.mark.parametrize("name, price, count, type, countPoints, device", [
        ('test_name1', 3001, 1, 'key', 1, CAR),
        ('test_name2', 3000, 167, '', 1, MOTO),
        # and so on, choosing different parameters
    ])
    def test_getItemTradePreferences(name, price, count, type, countPoints, appid):
            assert testing_funtion(price,count,type,countPoints,appid) == val_obj.getItemTradePreferences(name,price,count,type,countPoints)

然后我认为最好不要手动输入参数,而是编写一个随机使用的函数并为我做。

def generate_testing_values():
    return ['test_nameX', randint(0, 3000), randint(1, 1000), '', randint(1, 1000), choice([CAR, MOTO])]

并像这样调用测试:

@pytest.mark.parametrize("name, price, count, type, countPoints, device", [
        generate_testing_values(),
        generate_testing_values(),
        generate_testing_values(),
        # can I call generate_testing_values() in loop?
    ])
    def test_getItemTradePreferences(name, price, count, type, countPoints, appid):
            assert testing_funtion(price,count,type,countPoints,appid) == val_obj.getItemTradePreferences(name,
price,
count,type,
countPoints)

但我可以在decorator中的循环中以某种方式调用此generate_testing_values()吗? 我还没有找到解决方案,如果你知道请分享。

谢谢!

1 个答案:

答案 0 :(得分:0)

也许你应该构建gen​​erate_testing_values()函数来将所有值一起返回。