将动态生成的值传递给NUnit自定义属性 [英] Passing dynamically generated value to NUnit Custom Attribute
问题描述
对于我们的测试方案-基于应用程序的配置,我们可能要启用或禁用方案.为此,我创建了一个自定义的IgnoreIfConfig属性,如下所示:
For our test scenarios - based on configuration of the application, we may want to either enable or disable a scenario. For this purpose, I created a custom IgnoreIfConfig Attribute like this :
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?
Now This attribute would only take a constant string as an input. If I were to replace this with something generated dynamically at the runtime, Such as a value from Json file - How can I convert it to a Constant. Constant Expression, TypeOf Expression or Array Creation Expression of Attribute parameter type ? Such as Config.Enabled ?
推荐答案
按照查理的建议:我是这样实现的-
As per Charlie's suggestion : I implemented it like this -
PropCol pc = new PropCol(); // Class where the framework reads Json Data.
public IgnoreIfConfigAttribute(string config)
{
pc.ReadJson();
if(config = "TestCase") _config = PropCol.TestCase;
// Here TestCase is a Json element which is either enabled or disabled.
}
这篇关于将动态生成的值传递给NUnit自定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!