Swift Parameterized Testing Note

Swift Parameterized Testing

Apple introduced Swift Testing at WWDC24, which allows passing arguments to a test function using the @Test macro's arguments parameter. This feature enables parameterized testing, where the test function is called once for each value in the arguments collection. The Test Navigator displays the results of each test run. If two arguments are passed, Swift Testing generates test cases for all combinations of the two arguments. However, there is a limit of at most two arguments, and if not all combinations are needed, the arguments can be zipped to pair them. This feature is useful when testing a collection of input arguments that expect the same result. It can be particularly convenient when using a CaseIterable enum to drive the tests. The author found this feature useful when migrating XCTest-based unit tests to Swift Testing, especially when verifying attributes of Core Data managed object classes. The Swift Testing approach has advantages over using a for-loop, including independent test cases that can run in parallel and clearer failure reporting. Overall, parameterized testing in Swift Testing can simplify and combine tests, making it a useful feature in practice.