Unit Testing Basics

My blog post URL: https://www.peretang.com/basic-knowledge-of-unit-testing/


what is unit testing

    Unit tests, which verify that your application works as expected.

    Mainly through the Mock or Stub mechanism to isolate dependencies with other components/services, control the boundaries of testing, and perform code inspection and testing for individual classes or methods.

 

Why unit testing is needed

    Because each unit (class or method) is the cornerstone of the whole system.

    When unit testing is implemented, although it will affect production efficiency in the short term, the quality of the project will be improved after implementing unit testing, thus avoiding the cost of later maintenance and bug fixes, so from a long-term perspective, Implementing unit tests can help increase the productivity of your project.

    And due to the implementation of unit testing, problems will be found out in advance, changing the drawbacks that when there was no unit testing in the past, all problems were concentrated at the end of the project.

     From the above points we can conclude: unit testing = improved quality + increased productivity + reduced development and testing costs + improved workflow

 

The benefits of unit testing

    1. Brings higher test coverage: Unit tests can easily simulate error conditions and simulate various possible situations, thereby improving the robustness of the code.

    2. Improve team efficiency: Unit testing allows you to submit high-quality/tested code before "functional testing", avoiding problems during overall functional testing and thus avoiding the need for repeated revisions.

    3. Auxiliary supervision: A set of successfully passed unit tests can confirm that your code is running correctly, or remind you in time when you are doing something wrong.

    4. Reduce debugging: A good set of unit tests will reduce the chance and time spent debugging the application to find bugs.

    5. Give confidence to your refactorings: It would be very difficult to prove that a refactoring was successful without unit tests. Unit tests provide a safety net to provide confidence in your refactorings.

    6. Find out what needs to be improved: By managing unit tests, you can find out whether the code under test has design problems, whether the code needs to be improved, or whether it needs to be refactored.

    7. Provide perfect code examples: Unit tests can perfectly provide how the code in the application is used, and provide a usage example to other developers.

    8. Enable code coverage and other metrics: Provide data such as code coverage, code performance, application execution speed, etc.

 

Two mechanisms for unit testing to isolate dependencies

    Stub: Used to simulate an external dependency.

    Mock: Used to shield an external dependency.

    Note: In unit testing, we always use these two mechanisms to isolate external dependencies, but please avoid excessive isolation of dependencies, which will reduce the integrity of your tests, and cause your test code to also change when external dependencies change. not detected.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326066946&siteId=291194637