Desire XP-test

Source: Internet
Author: User

Here I want to discuss unit testing. Currently, unit test is becoming more and more popular among mainstream development platforms, including eclipse, netbeans, and visualstudio 2005, even in the team system, you can apply the policy of force unit test. In the future, WebLogic Workshop 9 may also be integrated. The importance of this policy can be seen in the industry. Although the benefits of unit test are self-evident, most project development in China still does not implement unit tests, many of them are forced to write unit tests under the Suppression of external policies to meet the literal test cases.CodeCoverage, trueProgramThere are only a few projects that members take the initiative to assign unit tests to their programs. One of the most important reasons is that most people think unit tests are a great waste of their development time and reduce work efficiency. In fact, since the practice of XP some time ago, I can clearly feel that unit test not only improves the software quality, but also improves the development efficiency. What does this mean? After reading this article, you will have the answer.

One problem that cannot be ignored is that, at present, many tests in the software development process only involve random data and carry out arithmetic operations. The so-called gross estimation of the number of test cases is also an individual phenomenon, I think this is not a successful test, and it will also lay a hidden risk for future project progress. To be honest, unit testing is not easy. Of course, if every class of my program implements addition and subtraction operations, unit testing may be quite well written, but in the real world, complicated business logic and a large number of database operations are absolutely mainstream. How can we conduct unit testing?

Unit Testing also has some principles. The first is testing first! No matter what features you want to implement, XP claims: Write test first. During the writing and testing process, the programmer learns and has a deep understanding of the business logic to be implemented, discovers questions, and asks for answers. This is the basic practice of test driven development (TDD). It can avoid the hidden danger of programmers blindly writing program code, the code written without full understanding or self-Thinking about the business logic is likely to be restructured or corrected one day in the future. Instead of paying for the workload at that time, it is better to calm down and consider how the program to be executed works, and what exceptions may happen.

The second is the principle of no dependency, that is, the running and results of any unit test do not depend on the external environment, these environments may include the configuration of operating system environment variables, data in the database, and running time. The results should be consistent whenever the Code remains unchanged. The most exposed data may belong to the database. In unit testing, before testing the code, prepare the data that may be required for testing, by writing the code for data preparation to the unit test, the effect can be achieved once and for all. One write and multiple executions also provide the possibility of automatic testing. Of course, designing these data is often very time-consuming. It is not easy to make enough data to measure all the relevant business logic, but it is precisely in this design process that, this fully tests the programmer's awareness of the demand, and avoids program vulnerabilities and dead ends to the greatest extent.

Then there is a trace-free principle, that is, the system status is consistent before and after the test, the database data is not increased or decreased due to the test, and all the data generated by the test is deleted. This is just as if the snake in MGS cannot leave any footprints to dive into. XP advocates that unit testing should not leave any traces, which is also an important guarantee for the consistency of automatic running results for multiple times. Therefore, when writing unit tests, the cleaning code must not be ignored. In particular, the data generated by the normal business logic code must find a way to grasp the features and delete them.

The last is the evolutionary principle, which is also the embodiment of XP's simple dogma in terms of testing. At any time, you should not expect to write all the test cases without flaws at the beginning. As experience grows, the quality of the first test cases will be improved, but it is not feasible to carry out excessive tests at the beginning. In the integration test, bugs that failed to be captured by unit test will inevitably be exposed. At this time, the original unit test will be modified, and verify that the new unit test is effective through the recession test (that is, the vulnerability of the original program is captured with the new test code. Unit Testing, like business code, is always on the path of continuous correction, restructuring, and evolution.

Here, we will summarize why unit testing improves the development efficiency. First, it writes the features of multiple automatic executions at a time, greatly reducing the workload of preparing and improving test data, second, when code refactoring occurs, it is sufficient to run all the unit tests once to ensure that the refactoring does not bring a destructive blow to the system. In this case, if there is no unit test, no one can clearly explain the possible consequences of a systemic restructuring or code modification. It is another huge workload for the Organization to manually test all modules that may be affected. It seems that unit tests allow programmers to write a lot more code. In fact, they greatly reduce the possible work intensity and make programmers more confident, so that they can have a thorough understanding of any code correction.

Of course, unit testing is only one aspect of testing, followed by interface testing, stress testing, and so on. I don't want to be partial, because I have taken the pains of focusing on unit testing and ignoring other tests, I would like to explain that the use of unit testing in domestic development practices is still quite lacking, but it can solve the most problems, it is hoped that more programmers will devote themselves to the unit test team out of their own needs.

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.