[翻译]NUnit---Property and Random Attributes(十四)
2013-12-08 15:49
218 查看
小记:由于工作琐碎,没得心情翻译而且也在看《CLR vis C#》,所以断更了差不多5个月,现在继续翻译,保证会翻译完成,不会虎头蛇尾。
另:NUnit已经更新到2.6.3版本,虽然正在开发2.9.6(据说会大版本更新为NUnit3.0),但是2.6.2的很多东西不会变,只不过会支持.NET4.5的某些新东西,所以笔者会在翻译2.6.2之后会保证也会翻译更新的给大家。也许翻译的不是很好或者不准确,仅给大家提供参考。有错误的地方,欢迎斧正。
废话不多说,上蒸菜:
PropertyAttribute (NUnit 2.4)
Property 特性使用键值对的形势,为任何测试用例或事例(fixture以后翻译为事例)提供了统一设置属性的方法。在下面的示例中,事例类MathTests赋予Location属性值为723,测试用例AdditionTest 赋予Severity 属性值 "Critical"
Example:
namespace NUnit.Tests { using System; using NUnit.Framework; [TestFixture, Property("Location",723)] public class MathTests { [Test, Property("Severity", "Critical")] public void AdditionTest() { /* ... */ } } }
使用注意事项:
NUnit自身并不使用Property特性,但会在XML输出文件和GUI测试属性对话框中显示。
同时,可以使用扩展来访问指定属性的值。也可以在测试中使用反射来访问属性值。
Custom Property Attributes
用户可以继承PropertyAttribute来自定义特性,NUnit可以自动识别。Property特性提供了一个protected修饰的构造函数,这个构造函数会使用这个属性值。NUnit自身的一些属性实际上是Property特性的变异。下面是示例会创建一个Severity属性。出了会有一个简单语法和类型安全之后,其余和一般属性一样。一个测试报告系统可能会利用这个属性来提供专业报告。
public enum SeverityLevel { Critical, Major, Normal, Minor } [AttributeUsage(AttributeTargets.Method, AllowMultiple=false)] public class SeverityAttribute : PropertyAttribute { public SeverityAttribute( SeverityLevel level ) : base( level ); } ... [Test, Severity( SeverityLevel.Critical)] public void MyTest() { /*...*/ }
从NUnit2.5开始,一个属性类特性可以包含多个键值对。这个功能没有对外公开,但是在派生属性类中可以使用,NUnit自身的某些特性使用了这个功能,如:RequiresThreadAttribute。
RandomAttribute (NUnit 2.5)
Random特性用于为一个参数化方法的一个参数提供一个随机组合值范围。NUnit会将每个参数的数据组合为一些了测试用例,所以如果为参数提供数据则必须为所以参数提供数据。一般默认NNunit的组合方法会将参数提供的数据进行所以可能的组合值。但可以在具体的测试方法使用指定的特性进行修改。
Random特性支持以下几种构造:
public Random( int count ); public Random( double min, double max, int count ); public Random( int min, int max, int count );
Example
以下的测试会执行15次,参数x有三个值,参数d会选择从-1.0到1.0的5个值,形成15次组合值[Test] public void MyTest( [Values(1,2,3)] int x, [Random(-1.0, 1.0, 5)] double d) { ... }
同理:
ValuesAttributeRangeAttribute
SequentialAttribute
CombinatorialAttribute
PairwiseAttribute
相关文章推荐
- [翻译]NUnit---Explicit and Ignore Attributes(十二)
- [翻译]NUnit---SetUp and SetUpFixture and Suite Attributes(十九)
- [翻译]NUnit---Maxtime and Ignore and Platform Attributes(十三)
- [翻译]NUnit---TearDown and SetUpFixture and Test Attributes(二十)
- [翻译]NUnit---Sequential and SetCulture and SetUICulture Attributes(十八)
- [翻译]NUnit---Culture and Datapoint Attributes(十)
- [翻译]NUnit---Category and Combinatorial Attributes(九)
- 待翻译---- Understanding Spring MVC Model and Session Attributes
- [翻译]NUnit---Action Attributes(八)
- [翻译]NUnit---Action Attributes(八)
- Property and Outlet
- Android Custom Views and XML attributes
- getattr and @property
- ios随机数,and()、random()、arc4random()
- CodeForces 482 D.Random Function and Tree(树形DP)
- InitializeObjectAttributes 【MSDN翻译】
- The Idea of Combining Random Matrix and Graphical Models in Machine Learning
- Agile PLM Setting Up EC Attributes and Attribute Mappin
- codeforces 668C - Little Artem and Random Variable
- OpenGL ES 3.0之VertexAttributes,Vertex Arrays,and Buffer Objects(九)