Why do unit testing

Why do unit testing

Usually we do any work will first consider its return, write code even more so. If the role of unit testing is not, no one would want to write a bunch of useless code, unit tests in the end be able to bring any advantage to us? as follows:

  • Easy to post reconstruction. Unit tests can provide protection to refactor the code, then refactor the code as long as all the unit tests run through, then to a large extent it represents the reconstruction does not introduce new BUG, ​​of course, this is based on the complete and effective unit testing Based on the coverage.
  • Optimize the design. Write unit tests will allow the user to view from the perspective of the caller, thinking, in particular, to develop ways to use the TDD-driven development, the program is designed to allow users to call and be easy to test, and decouple software.
  • Documentation. Unit testing is an invaluable document, a document showing how it is best to use a function or class of, this document is compiled, run, and it kept up to date, always synchronized with the code.
  • With a return to nature. After automated unit testing code appears to avoid the return, after their completion, anytime, anywhere to quickly run tests, rather than deploying code to the device, and then manually cover various execution paths, such behavior is low efficiency, waste of time.

And so on, talked about so many advantages, is nothing more than good interface design, correctness, may return to test and improve the documentation of calls, high cohesion, low coupling, these advantages have been enough for us to pay attention to the unit tests, but personally I feel that there are more important reasons.

  • First, to bring self-confidence. In taking over a new project, or is involved in a new project development, often this situation is that you go in halfway to participate, you need to interpret and understand the existing code structure for business understanding, codes for a understanding the relationship between the various modules. If you start to financial mistakes, it is likely the modified code will cause more BUG appeared, then they need to fix more of the BUG, ​​change a place, is likely to somehow affect another place, this phenomenon is very common. In another case, assuming you modify a function no problem, but need to test to verify, at the time the test will need to consider this point its original function test path which, but they need a function to verify the path to demonstrate this revision does not affect the functionality for existing points. This is where there is a considerable time cost, resulting in inefficient. So that if there is a way, I need to change where I want to change, and do not need to be concerned about the impact After modifying it causes, it does not need to be concerned about the return of the test, there is, at this time is when the unit test debut . Write unit test code, the code can make enough confidence in my own writing, it is tried and tested.
  • Secondly, faster feedback. For developers who have some programming experience, when he got a new demand when the first thought is not hands-Coding, but will first think about the structure of the code, some classes, this is how the data structure, and then began knock code. If there is no unit testing, the general process is the basic function of this module finished all began testing the use of functions such as MVP architecture. Usually start Model module, then perfect Presenter module, and finally write View module, and so these modules are finished, then run up APP, verify write their own functional modules are in line with demand, continue to go back and modify it did not meet the code, this center takes a long time to know the moment to write their own code to meet the requirements, are correct. There is no way that an instant feedback of it, yes, you can write unit tests, when you finish a function, immediately match a unit test function, so-write measurable way to ensure that you can write code on the spot immediately modified by a test, it means the completion of a small function point, finally, assembled the function, a function that we want a big point.
  • Finally, to save time. For Android development, it over and over again run APP, and then perform the appropriate user operation, to see whether the correct interface display, to test the functionality in this way, in fact, it is very time consuming and inefficient, and unit testing, almost can not open the APP to perform, of course, some need some resource file is needed APP operating conditions, most of the functions of the unit will be able to verify the completion of the testing phase, then the speed is relatively much faster. In addition, unit testing can help reduce BUG, ​​thereby reducing debugging time BUG, ​​and some low-level mistakes can be avoided in the unit testing phase out.

Excuse not to write unit tests

Many developers do not write unit tests, the most important reason is that they do not know what unit testing can bring benefits, or simply do not understand the word unit testing, it is naturally just like with parallel lines without intersection. There is a more important reason is that some developers programming ideas is still at a relatively early stage, the development of software just to achieve functional, within which high cohesion, low coupling, remodeling, design, testing, considered too specialized, for these terms and we do not understand the meaning, which naturally will not be considered for use. There are some reasons for non-ideological level, as follows:

  • Unit testing takes too much time. Software development so busy, got the time code could not finish writing unit tests. This is probably the most used excuse for developers, in some ways, it can not be considered an excuse, particularly in the work because it really put a lot of time developers. But is it you, you have not thought about the cause of overtime perhaps spent too much time on manual testing, debugging: Maybe you do not take into account the flexibility of the design, so that when changes occur in the requirements you need to spend a lot of time to complete a specific function in the heap complex code, such modifications they may introduce new BUG, ​​you need to turn lead to time-consuming manual testing, debugging, and so on, and so forth, the code will become increasingly the more chaos, more and more difficult to maintain, leading to endless overtime.
  • Testing is not my job. Test developers do not staff, but unit testing is indeed the work of developers, contains a variety of test, and unit testing is only scope of work for developers. Application developers to write code, so naturally need to ensure the correctness of the code, and unit testing is this white-box testing to ensure the correctness of the code, that is, carry out the purpose of the test in the case of the internal structure of the code to understand the logic, since When it comes to understanding the code, so developers naturally the most authoritative person. Consequently, write unit tests and submit the correct code to testers additional testing is the responsibility of the developer.
  • Code compiler, what also measured. In general, this is an excuse on the lips but could not keeping everything. Compile the code can only say that the code you write grammatically correct and does not represent guarantee correctness.
  • The original code is not a unit test, and difficult to test. The basic problem is to accept and maintain the code developed by others, and the original code itself does not have unit tests, and then add the code if the coupling is high, then the more difficult to write unit tests for the code. Reconstructing the code again after this time is when you know the code, is first added to test portions of the test unit, the test to ensure that these portions can not be contaminated, and there is sufficient knowledge of the code, the code to reduce the coupling resistance, and slowly added the test case, so that the coupling of the code, testability built up slowly.

Guess you like

Origin www.cnblogs.com/yeagen/p/11712903.html