This article is participating in small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
- 📢 welcome to like: 👍 collect ⭐ message 📝 if there are mistakes please correct, give people rose, hand left lingering fragrance!
- 📢 This article was originally written by Webmote and originally published by Nuggets.
- 📢 author’s motto: life is toss about, when you don’t toss about life, life will start to toss about you, let us come on together! 💪 💪 💪
1. What are unit tests
Unit testing is the test work of verifying the correctness of the smallest unit (function) of the program design.
In order to ensure the stability of the quality of the code, it is generally recommended to use unit testing to test the code, and cooperate with CI/CD to automate the work.
2. When to write unit tests
- If you’re writing a new feature,
- If you’re writing new features on untested code
- If you want to Fix a Bug
- If you want to Refactor untested code
- If you find an edge exception value
3. How to write unit tests
- Check into the reference
- Check out the cords
- Check boundary value
- External dependency state (on/off/file pointer/file properties/permissions)
- Data type, Initial Value Yes/No, Default value, overflow, underflow
- Abnormal test
- Understandability of error messages
- Branches, conditions, paths
4. How to write testable code
- Single responsibility principle: a class is responsible for one responsibility
- Open closed: open for extensions, closed for modifications
- Interface separation: Using multiple isolated interfaces is better than using a single interface
- Dependency inversion: Dependence on the abstract rather than the concrete
- Richter substitution: Wherever a base class can appear, a subclass must appear
Static classes and methods are difficult to unit test and often need to be converted into interface classes or wrapper classes
What is a good unit test
- fast
It is common to run thousands of unit tests on mature projects. You should spend very little time running unit tests. A few milliseconds.
- independent
Unit tests are stand-alone, can be run on their own, and do not depend on any external factors such as file systems or databases.
- repeatable
The results of running a unit test should be consistent, that is, always return the same results if nothing changes during the run.
- Since the inspection
Tests should be able to automatically detect test passes without any human interaction.
- Simple in a timely manner
Writing unit tests should not take too much unnecessary time compared to the code to be tested. If you find that testing your code takes a lot more time than writing it, consider a more testable design.
6. What not to write unit tests for
Services, glue code, and storage are generally strictly access code (characterized by simple sequential calls) that do not require unit testing. This code, on the other hand, has to deal with a lot of context-specific contexts to exchange data, so it is difficult to do unit tests and mocks a lot of interfaces. For example, storage context, cache context, network context, the maintenance cost of imitation is also very high, resulting in the failure to produce unit tests quickly, or even the implementation of the whole unit test failure!
The code that requires unit testing is always focused on the logic that the developer has written, not on the health of the environment. Therefore, it is easy to conclude that the test coverage of your code does not need to be 100%.
7. CI builds
Dotnet, Java, Vue, React, and others now support integration of unit tests. For example, dotnet can use dotnet test –collect:”XPlat Code Coverage” to output test results.
With Azure or GitLab, you can easily output test results.
Nodule 8.
Unit testing is hands-on. You don’t have to go for 100% code coverage at first, but once you get past 50%, your code is much more robust and maintainable.
👓 have seen this, but also care about a thumbs-up?
👓 has been liked, but also care about a collection?
👓 are collected, but also care about a comment?