【发布时间】:2017-11-17 16:15:13
【问题描述】:
对于我们的测试场景 - 根据应用程序的配置,我们可能想要启用或禁用一个场景。为此,我创建了一个自定义的 IgnoreIfConfig 属性,如下所示:
public class IgnoreIfConfigAttribute : Attribute, ITestAction
{
public IgnoreIfConfigAttribute(string config)
{
_config = config;
}
public void BeforeTest(ITest test)
{
if (_config != "Enabled") NUnit.Framework.Assert.Ignore("Test is Ignored due to Access level");
}
public void AfterTest(ITest test)
{
}
public ActionTargets Targets { get; private set; }
public string _config { get; set; }
}
可以如下使用:
[Test, Order(2)]
[IgnoreIfConfig("Enabled")] //Config.Enabled.ToString()
public void TC002_DoTHisIfEnabledByConfig()
{
}
现在这个属性只接受一个常量字符串作为输入。如果我用运行时动态生成的东西来替换它,例如来自 Json 文件的值 - 我如何将其转换为常量。属性参数类型的常量表达式、TypeOf 表达式或数组创建表达式?比如 Config.Enabled ?
【问题讨论】:
标签: attributes nunit constants typeof custom-attribute