Refactor tests to reduce code duplication.