Very glad to see this; having recently been bitten by several of the quirks of XCTest mentioned in the vision document. It's definitely the right time for a modern Swift testing library.
I'm also very glad to see that the plan is to use custom type metadata for test discovery; I think a lot of large codebases would benefit from the ability to define their own versions of the @Test
macro that handle specific use cases, like ensuring that all snapshot tests are automatically parameterized for multiple screen sizes, localizations, etc.
A few things I'd like to suggest:
Allow for Dynamic test parameterization
From the code samples I've seen so far, test authors have to know at compile time what the parameters of a test are. But sometimes it's impractical to gather this information. For example, I work in a code base with a lot of feature flags. This can lead to problems with unit tests; often, engineers forget to test a flag, or are unaware that their code is calling library code that is flagged. A project I've been hoping to tackle at some point is the ability to schedule a re-run of the test whenever it "discovers" new flag (where discovery is "the code under test attempting to access it"), with that flag set to a different value:
The only way I could do this in XCTest would be with a closure or some other form of macro:
func test_thing_with_many_flags() {
testingAllFlaggedCodePaths {
let result = myFunction() // `myFlag`, and various other flags I don't know about, might be called during this run
if myFlag.isOn {
XCTAssertEqual(result, something)
} else {
XCTAssertEqual(result, somethingElse)
}
}
}
It would be great if there were APIs to communicate back to the test runner that could be integrated into libraries (under #DEBUG
, of course) to make this possible.
Add the ability for tests to specify the way IDEs should show results to the user
Many tests failures are difficult or impossible to describe in words. For example, many iOS codebases use tests to verify view hierarchies which produce a bitmap image, which is then compared to a pre-recorded reference. A test of the Accessibility/VoiceOver attributes of a view hierarchy might want to produce an image calling out a specific section of a view hierarchy that is user interactable but has no accessibilityLabel
set.
Today, tests are limited to output like "references images not equal, run the command imagediff path-to-failure path-to-reference
.
It would be cool if this test library defined a protocol for locating and invoking something like a MacOS quick look preview, but for unit tests, which vendors could implement in their IDEs. The most basic implementation of this would be something similar to the LLDB debugQuickLookObject
which simply produces a bitmap. A more sophisticated system might allow the IDE to define what type of object it expected to receive: examples might include HTML in an IDE built with Electron, or an executable which produces a SwiftUI view hierarchy in an IDE built for MacOS with the native stack.