Refactor tests to extract common functionality