Nunit-Writing Tests
Nunit 测试可以被任意支持attributes的.net语言使用
- Attributes被用于去标识测试类和测试方法,然后通过不同的方式修改他们的行为
- Assertions针对一个或多个约束,测试一个实际值,然后断定为一个成功或失败的测试结果
- Test Context 写测试代码的时候,有时候需要测试上下文的一些信息
Attributes:
- Action
- Category
- Combinatorial
- Culture
- DatapointAttribute / DatapointsAttribute
- Description
- ExpectedException
- Explicit
- Ignore
- Maxtime
- Pairwise
- Platform
- Property
- Random
- Range
- Repeat
- RequiredAddin
- RequiresMTA
- RequiresSTA
- RequiresThread
- Sequential
- SetCulture
- SetUICulture
- SetUp
- SetUpFixture
- Suite
- TearDown
- Test
- TestCase
- TestCaseSource
- TestFixture
- TestFixtureSetUp
- TestFixtureTearDown
- Theory
- Timeout
- Values
- ValueSource
Assertions
NUnit 2.4以前,Assert 类的一个单独的方法被用于每个不同的断言,我们把这种方式叫作:Classic Model,相当于每类断言,一个方法,
Assert.AreEqual( int expected, int actual ); Assert.AreEqual( int expected, int actual, string message ); Assert.AreEqual( int expected, int actual, string message, params object[] parms ); Assert.AreEqual( uint expected, uint actual ); Assert.AreEqual( uint expected, uint actual, string message ); Assert.AreEqual( uint expected, uint actual, string message, params object[] parms ); Assert.AreEqual( decimal expected, decimal actual ); Assert.AreEqual( decimal expected, decimal actual, string message ); Assert.AreEqual( decimal expected, decimal actual, string message, params object[] parms ); Assert.AreEqual( float expected, float actual, float tolerance ); Assert.AreEqual( float expected, float actual, float tolerance, string message ); Assert.AreEqual( float expected, float actual, float tolerance, string message, params object[] parms ); Assert.AreEqual( double expected, double actual, double tolerance ); Assert.AreEqual( double expected, double actual, double tolerance, string message ); Assert.AreEqual( double expected, double actual, double tolerance, string message, params object[] parms ); Assert.AreEqual( object expected, object actual ); Assert.AreEqual( object expected, object actual, string message ); Assert.AreEqual( object expected, object actual, string message, params object[] parms ); Assert.AreNotEqual( int expected, int actual ); Assert.AreNotEqual( int expected, int actual, string message ); Assert.AreNotEqual( int expected, int actual, string message, params object[] parms ); Assert.AreNotEqual( long expected, long actual ); Assert.AreNotEqual( long expected, long actual, string message ); Assert.AreNotEqual( long expected, long actual, string message, params object[] parms ); Assert.AreNotEqual( uint expected, uint actual ); Assert.AreNotEqual( uint expected, uint actual, string message ); Assert.AreNotEqual( uint expected, uint actual, string message, params object[] parms ); Assert.AreNotEqual( ulong expected, ulong actual ); Assert.AreNotEqual( ulong expected, ulong actual, string message ); Assert.AreNotEqual( ulong expected, ulong actual, string message, params object[] parms ); Assert.AreNotEqual( decimal expected, decimal actual ); Assert.AreNotEqual( decimal expected, decimal actual, string message ); Assert.AreNotEqual( decimal expected, decimal actual, string message, params object[] parms ); Assert.AreNotEqual( float expected, float actual ); Assert.AreNotEqual( float expected, float actual, string message ); Assert.AreNotEqual( float expected, float actual, string message, params object[] parms ); Assert.AreNotEqual( double expected, double actual ); Assert.AreNotEqual( double expected, double actual, string message ); Assert.AreNotEqual( double expected, double actual, string message, params object[] parms ); Assert.AreNotEqual( object expected, object actual ); Assert.AreNotEqual( object expected, object actual, string message ); Assert.AreNotEqual( object expected, object actual, string message, params object[] parms );
从NUnit 2.4开始,一个新的constraint-based model被引进,这种通过传递一个约束对象给要执行的测试方法的方式用Assert类的单个方法为所有逻辑断言
Assert.That( object actual, IResolveConstraint constraint ) Assert.That( object actual, IResolveConstraint constraint, string message ) Assert.That( object actual, IResolveConstraint constraint, string message, object[] parms ) Assert.That( ActualValueDelegate del, IResolveConstraint constraint ) Assert.That( ActualValueDelegate del, IResolveConstraint constraint, string message ) Assert.That( ActualValueDelegate del, IResolveConstraint constraint, string message, object[] parms ) Assert.That( ref T actual, IResolveConstraint constraint ) Assert.That( ref T actual, IResolveConstraint constraint, string message ) Assert.That( ref T actual, IResolveConstraint constraint, string message, object[] parms ) Assert.That( bool condition ); Assert.That( bool condition, string message ); Assert.That( bool condition, string message, object[] parms ); Assert.That( TestDelegate del, IResolveConstraint constraint );
TestContext (NUnit 2.5.7 / 2.6)
每一个单元测试都运行在一个执行上下文中,这个执行上下文包含当前环境的一些信息以及测试本身的一些信息,我们可以通过TestContext 类来获取这样一些信息