How do I know if I am developing a good unit test?

It targets only the most important parts of your code base

Just as not all tests are equal, not all parts of your code base deserve the same attention in terms of unit testing. The value that tests bring is not only in how those tests themselves are structured, but also in the code they verify. It is important that you direct your unit testing efforts to the most critical parts of the system and verify the others only briefly or indirectly.

Avoid unit tests for getters and setters methods, unless they have a higher degree of complexity than they usually are.

This would be an example of a get method that should not be unit tested. But it is always important to ensure that the property is being tested in another type of test such as an e2e.

get expandMode(): any {
    return PanelBarExpandMode.Multiple;
}

This is an example of a get method that should be unit tested.

get col(): number {
  return this.currentColumn &&
    this.currentColumn !== '' &&
    this.visibleColumns.length > 0
    ? this.visibleColumns.findIndex(
        (element) => element.id === this.currentColumn
      )
    : -1;
}

Avoid unit tests for methods that only perform serverEvents emits.

@serverEvent(
  'HeaderClickCustomAction',
  GridEXExtractors.HeaderClickCustomActionArgsExtractor
)
headerClickCustomAction(event: EventData): void {
  this.HeaderClickCustomAction.emit(event);
}

Some of the tests, such as integration tests, can go beyond the domain model and verify how the system as a whole works, including non-critical parts of the code base. And that's fine.

Provides maximum value with minimum maintenance costs

The hardest part of unit testing is getting the maximum value with the minimum maintenance cost. It is not enough to incorporate tests into a build system. It is also crucial to keep in the suite only those tests whose value far exceeds their maintenance costs. This last attribute can be divided into two:

  1. Recognize a valuable test (and, by extension, a low-value test).

  2. Write a valuable test.

Last updated