我正在使用NUnit并在我的一些测试中应用category属性:
[Category("FastTest")]
Run Code Online (Sandbox Code Playgroud)
这些测试必须运行得非常快,不到10秒.所以我也装饰它们
[Timeout(10000)]
Run Code Online (Sandbox Code Playgroud)
现在的问题是:
每次我使用[类别("FastTest")]在幕后装饰方法时,如何使用[Timeout(1000)]自动装饰它?
创建一个继承自 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 属性。
| 归档时间: |
|
| 查看次数: |
3666 次 |
| 最近记录: |