XCTestManifests in generated swift package

Viewed 507

I am currently creating a Swift Package. By default, the package generated by Xcode contains the XCTestManifests file with the following code:

#if !canImport(ObjectiveC)
public func allTests() -> [XCTestCaseEntry] {
    return [
        testCase(TestPkgTests.allTests),
    ]
}
#endif

As you can see, this is the function that returns all test cases. However, I am curious when it will be called and in what case the condition !CanImport (ObjectiveC) will be satisfied.

2 Answers

This is needed for running tests on platforms other than macOS.

On macOS the test runner relies on the ObjectiveC runtime which is absent on other platforms, hence canImport(ObjectiveC) is false. The allTests() function is called from LinuxMain.swift file, you can generate it on macOS by running swift test --generate-linuxmain.

In newer SwiftPM releases there is an alternative option for running tests on those platforms: swift test --enable-test-discovery. You can read more about it here: https://forums.swift.org/t/pitch-enable-test-discovery-by-default/36619

Swift 5.4: Automatic Test Discovery Included

Swift 5.4 (April 26, 2021 release) includes test discovery by default.

Automatic test discovery is now the default on all platforms

swift test --enable-test-discovery is no longer needed.

The Swift 5.4+ swift package init template does not generate allTests and XCTestManifests.swift.

Related