Refactor tests and add test utility