Reduce code duplication in tests