Basic nunit unit testing knowledge

Source: Internet
Author: User

From: http://www.cnblogs.com/ywqu/archive/2009/11/06/1597669.html

 

Objective of implementing unit testing:

1. Reduce bugs and improve project quality

2. Develop good coding habits and improve developers' coding skills

 

What to test?

Minimal testable software elements (units), including the internal structure of units (such as logic and data streams) and the functions and observed behavior of the units.

 

Due to different development methods, unit testing is generally divided into the following methods:

 

1. Object-Oriented Software Development: Class (class) is the smallest unit of testing. The internal structure of the method is the focus of the test.

 

2. Structured Software Development: using modules (functions and processes) as the minimum unit for testing.

 

 

How to testHow?

White Box Test method: internal structure of the test unit, nunit nmock

Black box testing: Functions and Observability of test units-General Test Cases

 

Steps:

I. How to Design Unit Tests

 

It is necessary to clarify the functions implemented by the tested code and the corresponding logical relationship;

If... Else...

Switch... case...

While...

 

The input content of the test and the returned results must also be taken into account;

The Design of use cases should ensure that all aspects are covered and whether each path is covered.

To achieve full coverage, we need to analyze each function in detail and classify the analysis and discussion results into the relevant test libraries. It doesn't matter if the initial work is slow, as long as it can be done in great detail, it will be of great help for future testing. In future tests, you only need to directly call the original test class library and modify some simple statements to implement unit tests for the new module.

[Testfixture] indicates that the class contains the test code (this feature can be inherited ). This class must be public and have a default constructor.

[Test] indicates that it is a test method. The return value of the test method must be void and cannot contain parameters.

[Setup] attribute: used to identify a method and run it before all tests are started. It is used to initialize some resources, such as initialization classes, before a test.

[Teardown] attribute: used to identify a method and run it after all tests are completed to release some resources.

[Ignore] attribute: used to identify a method, indicating that this method does not need to be tested for some reason (for example, related code is not completed)

 

 

Ii. Use of nunit tools

 

Reference: nunit framework in vs2005

 

Iii. Common nunit classes and Methods

Assert (Assertions):

If the assertion fails, no method is returned and an error is reported.

If a method contains multiple assertions, all assertions after the asserted failure will not be executed. For this reason, it is best to use a try statement for each test asserted.

 

1. Test whether two parameters are equal

Assert. areequal (INT expected, int actual );

Assert. areequal (decimal expected, decimal actual );

....

 

2. Test whether two parameters reference the same object.

Assert. aresame (Object expected, object actual );

Assert. arenotsame (Object expected, object actual );

 

3. Test whether an object is contained by an array or list.

Assert. Contains (Object anobject, ilist collection );

 

Comparison assertions:

4. Test whether an object is larger than another object.

Assert. Greater (INT arg1, int arg2 );

 

5. Test whether an object is smaller than another object.

Assert. Less (INT arg1, int arg2 );

 

Type assertions:

Assert. isinstanceoftype (type expected, object actual );

 

Conditional test:

Assert. istrue (bool condition );

Assert. isfalse (bool condition );

Assert. isnull (Object anobject );

Assert. isnotnull (Object anobject );

Assert. isnan (double Adouble );

Assert. isempty (string astring );

Assert. isnotempty (string astring );

Assert. isempty (icollection collection );

Assert. isnotempty (icollection collection );

 

String assertions(Stringassert):Provides many useful methods for checking string values.

Stringassert. Contains (string expected, string actual );

Stringassert. startswith (string expected, string actual );

Stringassert. endswith (string expected, string actual );

Stringassert. areequalignoringcase (string expected, string actual );

 

Iv. attributes (Attribute):

Testfixtureattribute (nunit 2.0)

This feature marks a class that contains a test method and optional setup and teardown methods.

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.