【问题标题】:Call function, which randomize parameters for test, within decorators调用函数,在装饰器中随机化测试参数
【发布时间】:2017-09-30 07:20:29
【问题描述】:

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

    @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)

但是我可以在装饰器中以某种方式调用这个 generate_testing_values() 循环吗? 我还没有找到解决办法,如果你知道请分享。

谢谢!

【问题讨论】:

  • 你试过了吗?

标签: python loops testing decorator


【解决方案1】:

也许您应该构建 generate_testing_values() 函数以将所有值一起返回。

【讨论】:

    猜你喜欢
    • 2019-02-21
    • 1970-01-01
    • 2018-03-07
    • 2011-12-20
    • 2021-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多