Nunit unit test notes I-getting started

Source: Internet
Author: User

What is unit test?

Unit testing is a short section written by developers.CodeTo check whether a very small and clear function of the tested code is correct. Generally, a unit test is used to determine the behavior of a specific function under a specific condition (or scenario. For example, you may put a large value into an ordered list, and then confirm that the value appears at the end of the list. Alternatively, you may remove characters that match a certain pattern from the string and confirm that the string does not contain these characters.

 

The unit test is executed to prove that the behavior of a code segment is indeed consistent with the developer's expectation.

Is this test necessary for customers or end users, is it related to acceptance testing? This question is still difficult to answer. In fact, we do not care about the validation, verification, and correctness of the entire product here. Even now, we do not care about performance issues. All we have to do is to prove that the Code behavior is consistent with our expectations. Therefore, we want to test a very small and very independent functional piece. By building confidence in all the individual actions and making sure they are consistent with our expectations, we can start assembling and testing the entire system.

 

After all, if we are not sure whether the code we are writing is consistent with our expectations, other forms of testing can only be a waste of time. After the unit test, you also need other forms of testing. It may be a more formal test, and everything depends on the needs of the environment. In short, testing is like doing good deeds, always starting from the home (the most basic correctness of the Code.

 

Why unit test?

 

Unit testing will not only make your work easier, but also make your design better, and even greatly reduce the time you spend debugging.

Generally, we get involved in the trouble of assuming that the underlying code is correct. First, the underlying code is used in the high-level code, and then the high-level code is used by the higher-level code, so reciprocating. Without any confidence in the behavior of the code, we waited for the assumption that we had built a house with a erected card -- just move the following card gently, the whole house collapsed.

 

When the underlying code is no longer reliable, the necessary changes cannot be limited to the underlying code. Although you can fix the underlying problems, these modifications to the underlying code will inevitably affect the high-level code, so the high-level Code also needs to be modified; in this way, recursive, it is likely to move to a higher level of code. Therefore, a correction to the underlying code may lead to a series of changes to almost all the code, making the modifications more and more complex. As a result, the whole house made up of cards collapsed, and the entire project ended in failure.

 

Pat always said, "how is this possible ?" Or, "I really don't understand why ". If you find that you sometimes have this idea, it is usually because you do not have enough confidence in your code-you are not sure what is working normally and what is not.

 

To get the confidence that Dale has in the code, you need to "ask" what the code has done and check whether the results are exactly the same as you expected.

This simple idea describes the core of unit testing: this simple and effective technology is designed to make code more perfect.

 

What do I need to do

 

Introducing unit tests is simple because it is fun. However, during project delivery, we still provide product code to customers and end users, without the code for unit testing. Therefore, we must have a full understanding of the purpose of unit testing. First and foremost, unit testing is used to make your work-and the work of your teammates-easier.

Is its behavior consistent with my expectation?

Most fundamentally, you need to answer the following question: "has this code achieved my goal? "Maybe what the Code does is wrong as far as the requirement is concerned, but that is another problem. What you want is the code you want to prove that what it does is what you expect.

Is its behavior consistent with my expectation?

Many developers say they write a test. That is to say, let all the code run once from start to end and only test a correct execution path of the Code. As long as this process is easy to change, there is no problem, and the test is complete.

You cannot test a bridge like this: on a windy day, just let a car drive over the bridge. Obviously, this test is far from enough for bridge testing. Similarly, when testing whether the behavior of a piece of code is consistent with your expectation, you need to confirm that the code is consistent with your expectation under any circumstances; for example, when the wind is high, the parameters are suspicious, the hard disk has no space left, and the network is offline.

Does unit test show my intention?

For unit testing, the most gratifying unexpected receipt is that it helps you fully understand the usage of code. In terms of effectiveness, unit testing is like a document that can be executed, which describes the functions you can expect to complete when you call code using various conditions.

How to perform Unit Testing

Unit testing is a simple and easy-to-learn technique. However, if you can follow some guiding principles and basic steps, learning will become easier and more effective.

The first thing to consider is how to test the available methods before writing these test methods. With such a general idea, you can compile the test code itself when writing the implementation code or before.

Next, you need to run the test itself, or all other tests of the system module at the same time, or even the test of the entire system, provided that these tests run relatively fast. Here, we need to ensure that all tests can pass, not just newly written tests. That is to say, to ensure that no direct bug is introduced, you must ensure that other tests will not be damaged.

First unit test

Let's start with a simple demo:

1. Download the tool nunit, an open-source software for unit testing, which provides a test framework and a test run.Program. By default, it is installed in the c: \ Program Files \ directory. After installation, you can find it in the Start Menu.

2. Create a class library project named unittest. Create a largest. CS file and a testlargest. CS file. The Code is as follows:

 

Namespace unittest {public class CMP {public static int largest (INT [] list) {int index, max = 0; If (list. length = 0) {Throw new exception ("em") ;}for (Index = 0; index <list. length; index ++) {If (list [Index]> MAX) {max = list [Index] ;}} return Max ;}}}
 
Namespace unittest {[testfixture] public class testlargest {[test] public void largestof3 () {assert. areequal (9, CMP. largest (New int [] {7, 8, 9 }));}}}

3. Find the nunit you just installed in the Start menu to open it. Check tool -- setting -- and Visual Studio on the interface, open a project, that is, a compiled DLL file, and run it to get the result.

 

4. When you run a selected test, the GUI displays a large, colored status bar. If all tests pass, the status will show happy green; if any test fails, the status bar will become angry red. If the status bar is yellow, some tests are skipped (details will be described later ).

 

5. what we see here is the ideal state, if we change the data from the previous test to assert. areequal (-7, CMP. largest (New int [] {-9,-8,-7}); then, you can try it.

The result of the test is a red color, and the description is as follows:

 
Unittest. testlargest. largestof3: expected:-7but was: 0

It tells you that it should have been-7, and the result is indeed 0. As you know, we should initialize Max to the minimum minvalue of int during initialization.

 

In this way, a simple unit demo is over.

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.