Third time job

Source: Internet
Author: User

Unit Test

Unit testing is the test of correctness checking for the minimum module of the software. The so-called minimal module, in the context of OOP, is usually referred to as the method of the object.

Unit testing is a basic function and basic skill of the programmer, which directly affects the productivity of the programmer and the quality of the software.

Also directly affect the team's cooperation. Consider the following scenario:

A programmer submitted a piece of untested code, another programmer checkout out, so that the local code does not work, it took a long time to discover that this untested code problem: affecting the work of other programmers.

A programmer's code did not unit test, after a period of time to find the program run abnormal, after hard troubleshooting, found that this code problem, and then fixed the vulnerability; After a while, found another bug, and then fixed the new bug, but when the bug was fixed, The bug that caused the last discovery (which was not found at the time because it was generally just a simple test of the bug that was just fixed): This is a vicious follow-up, and a bug fix causes another bug to appear unconsciously.

The benefits of unit testing are also obvious:

Improves the productivity of the entire team. A quality code is a contribution to the entire team.

Improve the programmer's sense of responsibility and the quality of the code. By unit testing, to ensure that your code is reliable and complete, it is responsible for itself, but also for the team.

Content of unit tests
Unit tests are mainly divided into manual static checks and dynamic (automatic) execution of trace checks.

Manual static Check
The main purpose of the manual static check is to ensure:

To achieve the desired function.

Clarity, code is easy to understand, comments are unambiguous.

Normative, code format, naming, etc. in line with the company's coding standards and code style.

Efficiency, and code execution is as high as possible.

The priority of the above check target is from high to the end.

Automatic tracking Check
The automatic tracking check is the focus of our research. The so-called automatic tracking check, refers to the unit testing tools, writing unit test classes and methods. When the test class and method are executed, the set input conditions are calculated automatically and the results of the test are given.

With automated tracking tests, we were able to write each bug we found or possible future bugs into our test classes and methods. This allows you to effectively discover whether a bug still exists, or if the bug that has been fixed is "resurgent" as long as you run the test class.

Unit testing of automatic tracking checks is one of the core concepts of XP. The combination of unit testing and daily build technology will ensure smooth progress throughout the development process.

Unit testing with JUNIT4 in eclipse

The first step:

Open Eclipse.exe, create a project named "123" and create a class named "UnitTest".

 Public class unittest {publicstaticvoid  main (string[] args) {int sum=0  ; int a=2;  for (int i = 0; i < i++) {sum+ =a;a+=2;} SYSTEM.OUT.PRINTLN (sum);}}

Step Two:

introduce the JUNIT4 Unit test package into this project: Right-click on the item "123", click "Properties", and in the pop-up Properties window, first select "Java Build Path" on the left, then go to the right to select the "library (L)" (Libraries) tab, then on the far right click on the "AddLibraries" button, click Finish,JUNIT4 package is included in our project. as shown in the following:

Step Three:

generate JUnit test framework: In Eclipse's Package Explorer, right-click on the Class "UnitTest" pop-up menu and select New →junit test Case (new a JUnit Test case), in the popup dialog box, make the appropriate selection. As shown in the following:

The complete test code is as follows:

Import Staticorg.junit.assert.*;Importorg.junit.Test; Public classunittesttest {@Test Public voidTestmain () {intSum=0; inta=2;  for(inti = 0; I < 50; i++) {sum+=A; A+=2;        } System.out.println (sum); }    }

The test results are as follows:

The test was successful and no error occurred.

Third time job

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.