What is a unit test?
Takes a very small piece of testable code and determines whether it behaves as expected. The size of the unit under test is not strictly defined, however unit tests are typically written at the class level or around a small group of related classes. The smaller the unit under test the easier it is to express the behaviour using a unit test since the branch complexity of the unit is lower.
If it is difficult to write a unit test, this can highlight when a module should be broken down into independent, more coherent pieces and tested individually.
Unit testing is a powerful design tool in terms of code and implementation, especially when combined with TDD.
What can/should you unit test?
- Test the behaviour of modules by observing changes in their state. This treats the unit as a black box tested entirely through its interface.
- Look at the interactions and collaborations between an object and its dependencies. These interactions and collaborations are replaced by mocks/stubs etc.
Purpose of unit tests
- Constrain the behaviour of the unit
- Fast Feedback