Add before and after hooks to tests

This is a work in progress to add proper unit tests for all the functionality.