1 minute read

Unit tests are what programmers use to make sure that a code unit is working as expected.

A code unit can be a function, a method , a procedure, a class, etc.

The idea is to test every unit for efficiency and effectiveness separately

Making sure that the business logic works is not enough, among other things tests should check the initial state, control variable types, check what will be returned and confirm that there are no problemas in the final state.

ideally, having the unit tests will simplify the work when doing the integration test.

Features

  • Automatic: No manual intervention should be required. This is especially useful for continuous integration.
  • Complete: Must cover as much code as possible.
  • Repeatable: You should not create tests that can only be executed once. It is also useful for continuous integration.
  • Independent: The execution of one test should not affect the execution of another.
  • Professional: The tests should be considered the same as the code, with the same professionalism, documentation, etc.

Benefits

  • Encourage change: Unit tests make it easier for the programmer to change the code to improve its structure (refactoring), since they allow to test the changes and thus make sure that the new changes have not introduced defects .
  • Simplify integration: They allow to reach the integration phase with a high degree of security that the code is working correctly. This makes integration testing easier.
  • Document the code: The tests themselves are documentation of the code, since you can see how to use it there.
  • Separation of the interface and the implementation: Since the only interaction between the test cases and the units under test are the interfaces of the latter, you can change either one without affecting the other, sometimes using mock objects (mock object - mockup) that enable the behavior of complex objects in an isolated (unitary) way.
  • The errors are more limited and are easier to locate: Since we have unit tests that can unmask them.

Limitations

Unit tests will not discover integration errors, performance problems, and other problems that affect the entire system as a whole.

Furthermore, it may not be trivial to anticipate all the special cases of inputs that the program unit under study may actually receive.

Unit tests are only effective when used in conjunction with other software tests.