NUnit重试动态属性 [英] NUnit retry dynamic attribute

查看:51
本文介绍了NUnit重试动态属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我想从app.config值动态传递重试次数.

Hello I want to pass the number of retries dynamically from app.config value.

app.config包含以下行:

The app.config has the following line:

<add key="retryTest" value="3"/>

我已经定义了此变量:

public static readonly int numberOfRetries = int.Parse(ConfigurationManager.AppSettings["retryTest"]);

最后,我想将该变量作为参数传递给Retry NUnit属性:

Finally I would like to pass that variable as a parameter to Retry NUnit attribute:

[Test, Retry(numberOfRetries)]
public void Test()
{
    //.... 
}

但是出现以下错误:

属性参数必须是一个常量表达式,typeof属性参数的表达式或数组创建表达式类型"

"An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type"

如何动态传递该值?

推荐答案

虽然我并不完全了解

While I am not fully aware of the RetryAttribute. One possible way of achieving the desired functionality would be to extend its current functionality.

/// <summary>
/// RetryDynamicAttribute may be applied to test case in order
/// to run it multiple times based on app setting.
/// </summary>
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
public class RetryDynamicAttribute : RetryAttribute {
    private const int DEFAULT_TRIES = 1;
    static Lazy<int> numberOfRetries = new Lazy<int>(() => {
        int count = 0;
        return int.TryParse(ConfigurationManager.AppSettings["retryTest"], out count) ? count : DEFAULT_TRIES;
    });

    public RetryDynamicAttribute() :
        base(numberOfRetries.Value) {
    }
}

然后应用自定义属性.

[Test]
[RetryDynamic]
public void Test() {
    //.... 
}

这篇关于NUnit重试动态属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆