Refactor duplicate code in tests.