Pragmatic unit testing in C #-Part1

Source: Internet
Author: User

  1. 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 ));

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.