Nunit-Writing Tests

时间:2023-12-11 11:27:32

Nunit 测试可以被任意支持attributes的.net语言使用

  1. Attributes被用于去标识测试类和测试方法,然后通过不同的方式修改他们的行为
  2. Assertions针对一个或多个约束,测试一个实际值,然后断定为一个成功或失败的测试结果
  3. Test Context 写测试代码的时候,有时候需要测试上下文的一些信息

Attributes:

  1. Action
  2. Category
  3. Combinatorial
  4. Culture
  5. DatapointAttribute / DatapointsAttribute
  6. Description
  7. ExpectedException
  8. Explicit
  9. Ignore
  10. Maxtime
  11. Pairwise
  12. Platform
  13. Property
  14. Random
  15. Range
  16. Repeat
  17. RequiredAddin
  18. RequiresMTA
  19. RequiresSTA
  20. RequiresThread
  21. Sequential
  22. SetCulture
  23. SetUICulture
  24. SetUp
  25. SetUpFixture
  26. Suite
  27. TearDown
  28. Test
  29. TestCase
  30. TestCaseSource
  31. TestFixture
  32. TestFixtureSetUp
  33. TestFixtureTearDown
  34. Theory
  35. Timeout
  36. Values
  37. 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 类来获取这样一些信息