Basic Nunit unit testing knowledge

Source: Internet
Author: User

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.