Friday, December 26, 2008

AAAA Unit Testing

A while back Oren wrote about the Arrange, Act, Assert syntax for unit tests. I've seen several other discussions on this and I am not sure if he is the originator but I'll attribute him either way.

The syntax I most commonly use ressembles an Arrange, Assert, Act and Assert (AAAA) pattern.
  • Arrange the system in it's initial configuration. Create any collaborators and mocks you may need. Initialize services.
  • Assert that the initial state is correct. For me this is important since so many layer of dependencies exist in modern applications. This is analogous to a control group in the scientific method. In this step I would also test the inverse of later assertion such as the non-existence of objects that should only exist after the Act step.
  • Act on the system by creating/deleting/updating new objects or invoking services.
  • Assert again that the system is in it's expected state.
I've caught a lot of bugs using this method. Asserting the initial state may also be a good way to expose weakness' in the design.

No comments: