NUnit属性

Ere*_*rez 6 .net c# nunit

我正在使用NUnit并在我的一些测试中应用category属性:

[Category("FastTest")]
Run Code Online (Sandbox Code Playgroud)

这些测试必须运行得非常快,不到10秒.所以我也装饰它们

[Timeout(10000)]
Run Code Online (Sandbox Code Playgroud)

现在的问题是:

每次我使用[类别("FastTest")]在幕后装饰方法时,如何使用[Timeout(1000)]自动装饰它?

Ric*_*est 0

创建一个继承自 TimeoutAttribute 的自定义属性 FastTest。然后,该属性将实现其自己的类别命名约定。像这样的东西应该有效

public class FastTestAttribyte :TimeoutAttribute
{
    protected string categoryName;

    public FastTestAttribyte (int timeout):base(timeout)
   {
       categoryName = "FastTest";
   }

    public string Name { get return categoryName; }
}
Run Code Online (Sandbox Code Playgroud)

编辑

如果你反编译该属性,它将起作用,这就是它的作用

[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class | AttributeTargets.Assembly, AllowMultiple=true, Inherited=true)]
public class CategoryAttribute : Attribute
{
    // Fields
    protected string categoryName;

    // Methods
    protected CategoryAttribute()
    {
        this.categoryName = base.GetType().Name;
        if (this.categoryName.EndsWith("Attribute"))
        {
            this.categoryName = this.categoryName.Substring(0, this.categoryName.Length - 9);
        }
    }

    public CategoryAttribute(string name)
    {
        this.categoryName = name.Trim();
    }

    // Properties
    public string Name
    {
        get
        {
            return this.categoryName;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为 Nunit 会使用反射来反射属性的 Name 属性。

  • 但这个属性将仅表现为 TimeoutAttribute,即使添加这样的猫名称,nunit UI 也不会理解它必须使用 Name 作为类别,我猜......还是我错了?! (3认同)