- Classic asserts
Asserts are the fundamental building block for unit tests; the nunit Library provides a number of different forms of assert as static methods in the assert class.
Areequal
Assert. areequal (expected, actual [, string message])
Assert. areequal (expected, actual, tolerance [, string message])
Less/greater
Assert. Less (x, y)
Assert. Greater (x, y)
Greaterorequal/lessorequal
Assert. greaterorequal (x, y)
Assert. lessorequal (x, y)
Isnull/isnotnull
Assert. isnull (object [, string message])
Assert. isnotnull (object [, string message])
Aresame
Assert. aresame (expected, actual [, string message])
Istrue
Assert. istrue (bool condition [, string message])
Assert. isfalse (bool condition [, string message])
Fail
Assert. Fail ([String message])
2. Constraint-based asserts
Is. Unable
Assert. That (actual, is. Conflict to (expected ))
Assert. That (actual, new pair constraint (expected ))
Here is one calledWithin ()That is equivalant to our same example that used the classic-style in the previous section.
Assert. That (10.0/3.0, is. Failed to (3.33). Within (0.01f ));
Is. Not. Unable
Assert. That (actual, is. Not. Failed to (expected ))
Assert. That (actual, new notconstraint (New effecconstraint (expected )));
Is. atmost
Assert. That (actual, is. atmost (expected ))
Is. null
Assert. That (expected, is. null );
Assert. That (expected, is. Not. null );
Assert. That (expected ,! Is. null );
Is. Empty
Assert. That (expected, is. Empty );
Is. Atleast
Assert. That (actual, is. Atleast (expected ));
Is. instanceoftype
Assert. That (actual, is. instanceoftype (expected ));
Has. Length
Assert. That (actual, has. Length (expected ));
3.More nunit asserts
List. Contains
Assert. That (actualcollection, list. Contains (expectedvalue ))
Assert. That ({5, 3, 2}, list. Contains (2 ))
Is. subsetof
Assert. That (actualcollection, is. subsetof (expectedcollection ))
Assert. That (New byte [] {5, 3, 2}, is. subsetof (New byte [] {1, 2, 3, 4, 5 }))
Text. startswith
Assert. That (actual, text. startswith (expected ))
Assert. That ("header: data.", text. startswith ("header :"))
Assert. That ("header: data.", text. startswith ("Header"). ignorecase)
Text. Matches
Assert. That (actual, text. Matches (expected ))
Assert. That ("header: data.", text. Matches ("$ header ^ \."))
Fileassert. areequal/arenotequal
Fileassert. areequal (fileinfo expected, fileinfo actual)
Fileassert. areequal (string pathtoexpected, string pathtoactual)
Test whether two files are the same, byte for byte. Note thatIf we do the work of openingStream(File-based, or not), weCan useEqualsconstraintInstead, like so:
Stream expectedstream = file. openread ("Expected. bin ");
Stream actualstream = file. openread ("Actual. bin ");
Assert. That (actualstream, is. Conflict to (expectedstream ));