What are the best practices of unit testing?

 What is Unit testing?

Small pieces of code known as "units" are tested. A unit can be a function, module, class etc. When unit testing is being conducted, the application is not connected with external dependencies such as HTTP services, the filesystem or databases. This makes unit tests to be more stable and fast.

Unit testing best practices:

1. The importance of test names: The intent of the test case is reflected explicitly by a good unit test name. Consistent naming conventions should be followed. Code readability is supported by writing good test names, which will make it easier to extend the code.

2. Automated tests need to be set up: Unit testing framework can be used for carrying out automated unit testing. Tests can also be automated in the CI/CD pipeline. Testing units manually is considered to be a tedious process and is also considered to be less reliable.

3. Deterministic tests need to be written: False negatives and positives are common in software testing and hence the objective is to minimize them. Consistent outputs for tests is the goal, so that the desired function can be verified. Hence, unit tests should be deterministic. A consistent behavior is expected out of a deterministic test, each time the test is being run.

4. Test dependencies should be reduced: There should be no dependency of tests on each other. When dependencies are reduced between units, the tests can be simultaneously run by test runners on different pieces of code. If the unit dependencies are staged within the test code then only it is considered to be testable. No external dependencies or real-world should affect the test outcome.

5. Test coverage should be maximized: There may be a goal to attain 100% test coverage, but, it is not always considered to be feasible. There may be time and budget constraints for carrying out such comprehensive testing activities. It is also considered to be theoretically impossible in certain scenarios. Hence, the maximum coverage possible should be worked out.

Comments

Popular posts from this blog

Python Testing Framework – An Informative Guide