Refactor test code for reusability