【问题标题】:Passing dynamically generated value to NUnit Custom Attribute将动态生成的值传递给 NUnit 自定义属性
【发布时间】: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


    【解决方案1】:

    你不能按照你的要求去做,但你可以以不同的方式看待问题。只需在要检查的 JSON 文件中为属性指定某个属性的 name,例如“配置”。

    【讨论】:

    • 谢谢您,先生,这绝对帮助我少思考开箱即用,并为测试用例实施配置检查。感谢您对此的快速响应。
    【解决方案2】:

    根据查理的建议:我是这样实现的 -

    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.
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-23
      • 1970-01-01
      • 2010-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-26
      • 1970-01-01
      相关资源
      最近更新 更多