Contents

CustomTestStringConvertible

A protocol describing types with a custom string representation when presented as part of a test’s output.

Declaration

protocol CustomTestStringConvertible

Overview

Values whose types conform to this protocol use it to describe themselves when they are present as part of the output of a test. For example, this protocol affects the display of values that are passed as arguments to test functions or that are elements of an expectation failure.

By default, the testing library converts values to strings using String(describing:). The resulting string may be inappropriate for some types and their values. If the type of the value is made to conform to CustomTestStringConvertible, then the value of its testDescription property will be used instead.

For example, consider the following type:

enum Food: CaseIterable {
  case paella, oden, ragu
}

If an array of cases from this enumeration is passed to a parameterized test function:

@Test(arguments: Food.allCases)
func isDelicious(_ food: Food) { ... }

Then the values in the array need to be presented in the test output, but the default description of a value may not be adequately descriptive:

 Test case passing 1 argument food  .paella to isDelicious(_:) started.
 Test case passing 1 argument food  .oden to isDelicious(_:) started.
 Test case passing 1 argument food  .ragu to isDelicious(_:) started.

By adopting CustomTestStringConvertible, customized descriptions can be included:

extension Food: CustomTestStringConvertible {
  var testDescription: String {
    switch self {
    case .paella:
      "paella valenciana"
    case .oden:
      "おでん"
    case .ragu:
      "ragù alla bolognese"
    }
  }
}

The presentation of these values will then reflect the value of the testDescription property:

 Test case passing 1 argument food  paella valenciana to isDelicious(_:) started.
 Test case passing 1 argument food  おでん to isDelicious(_:) started.
 Test case passing 1 argument food  ragù alla bolognese to isDelicious(_:) started.

Topics

Instance Properties

See Also

Retrieving information about checked expectations