Step 1: unit test

Source: Internet
Author: User

I have been writing unit tests in the company for more than two months (I have had a hard time thinking). To tell the truth, it is a bit like catching up with a duck and doing it at the end of the project, obviously, its role has already been a real name, and I am still looking for new users like me to write (I was not very familiar with the business process at the beginning), so I have been trying to learn Unit Testing Well, write it and do your own thing well!

I am also reading the programmer's practice-unit test book, read the various articles in the garden, and write them out to share with you!

Ebook: http://www.kuaipan.cn/file/id_29568238492847284.htm

Testing is the beginning and end of the entire software project. A good test ensures the quality of the software and reduces the workload of searching for bugs, therefore, as a developer, we need to understand various testing procedures and core ideas. However, this unit test is a required skill for our developers. This time, we will go to unit test )!

 1. My initial understanding of Unit Testing

① Keep writing unit tests in development and cultivate them into a habit!

② Write efficient unit tests. This kind of ability needs to be accumulated in practice!

③ Improve your understanding of unit testing and treat it like coding!

 

 2. What is unit test?

In general, unit testing is to check whether the returned results of a function after execution or its impact on system data (or other factors) are consistent with your expectations, that is to prove that the Code behavior is consistent with what I expected!

 

 3. Why should we use unit tests?

① The most direct reason is to ensure the correctness of our function. If this function is called by the upper-Layer Code without the guarantee, the calling level will become deeper and deeper as the project goes deeper, it is easy to produce serious bugs, which increases the development difficulty and reduces the development efficiency!

② The root cause is to reduce the workload of our developers and make our work easier (this is just a relative statement )!

 

 4. connotation of Unit Testing

If you increase the unit test to a certain extent, it can make our code more perfect and concise!

 

 5. Nature of Unit Testing

Remember that, instead of writing unit tests for work, unit tests are convenient for our developers and make our work easier!

Unit Testing can reduce the amount of time we spend solving unnecessary bugs (not to say there are no bugs, but to reduce unnecessary bugs) and focus a lot of time on business needs!

 

 6. Is the function Behavior consistent with expectation?

If the test only takes the correct impact into account in the correct environment, the unit test will fail!

All aspects of the test should be taken into account, such as environmental factors (or the environment in which the system is located), various exceptions, and boundary values, therefore, we should try our best to consider special cases to achieve full confidentiality (do your best )!

 7. Do I need to rely on unit tests?    

The answer is yes!

When you are confident that the function you write is absolutely correct and there is no basis for testing the code, unexpected errors may often occur, because you may neglect other situations, therefore, it is necessary to write unit tests to ensure the accuracy of our functions!

Note: The following describes what tests should be performed!

  

8. What is unit test and its role?

① The most direct thing is to ensure the correctness of the function (everyone knows )!

② We can also determine what this function is used for based on unit tests. That is to say, unit tests are similar to an executable document, by looking at unit tests, other developers can understand what the function you are testing is!

 9. How to perform unit testing?

Here we use the unit test framework that comes with vs2008 and 2010!

① Use unit test provided by Vs, which is easy to learn!

② The test should be comprehensive!

③ Ensure that all tests can pass, regardless of the old or new test code!

④ Ensure that all tests do not affect any modules in the system (this is important )!

⑤ Run the test code in time and view the running results to ensure that the system runs normally!

    

 10. Do not make excuses for not writing test code   

In general, it seems that when you write a function that does not need to be modified, you should write the unit test code for this function!

In most cases, developers spend a lot of time modifying bugs. If you can perform unit tests as early as possible, it will reduce your workload (although it is impossible to have no bugs, but writing unit test is helpful to you.) even if a bug occurs in the future, I think you can quickly locate the location where the bug is generated!

The unit test should not be written at the end of the project. If the person who writes the unit test is still a newbie to the project, writing the unit test is nonsense, this method does not reflect the core concept of unit testing, and I am in this status. I am really scared, so I still need to work harder to understand the system process!

 

 11. If there is really no time to write a unit test, consider the following questions?

① How long have you spent debugging the code you wrote?

② You currently think that your code is correct, but it is likely that there are serious risks in the system. Have you spent a lot of time looking for these risks?

③ How long have you spent locating a new bug in the source code?

Conclusion: As the project goes deeper, your function will be called more and more deeply, so special cases will happen frequently. In case of any situation, you will spend a lot of energy to solve it, on the other hand, proper unit test code will greatly reduce your workload. This is proven in practice!

 

Conclusion: There are so many basic unit test ideas. Hope you can give us some advice. The unit test series will be updated continuously!

Next article: Step 2 of unit test: What aspects should the Test start from?

Step 3: Create your own unit test

Step 4: unit test tool → mole Technology

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.