Refactor common functionality for tests